0% found this document useful (0 votes)
18 views4 pages

Subquery Practice 1729134327

The document provides a series of SQL queries related to subqueries, showcasing various use cases such as retrieving employees with salaries above average, finding the second highest salary, and identifying departments without employees. Each question is accompanied by a SQL solution that demonstrates how to implement the subquery concept effectively. The queries cover a range of scenarios, including employee management, department analysis, and order tracking.

Uploaded by

anmol kabra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Subquery Practice 1729134327

The document provides a series of SQL queries related to subqueries, showcasing various use cases such as retrieving employees with salaries above average, finding the second highest salary, and identifying departments without employees. Each question is accompanied by a SQL solution that demonstrates how to implement the subquery concept effectively. The queries cover a range of scenarios, including employee management, department analysis, and order tracking.

Uploaded by

anmol kabra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ɪᴍᴘᴏʀᴛᴀɴᴛ 𝘘ᴜᴇꜱᴛɪᴏɴꜱ ʀᴇʟᴀᴛᴇᴅ ᴛᴏ ꜱᴜʙ𝘘ᴜᴇʀʏ ɪɴ ꜱ𝘘ʟ:

1. What is a Subquery? Explain with an example.

Solution: A subquery is a query within another query. The result of the subquery is used by the
main query.

sql:
SELECT employee_id, name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
This query retrieves employees who earn more than the average salary.

2. How can you use a subquery to fetch the second highest salary from a table?

Solution:

sql:
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
3. Write a query to find employees who earn more than the average salary in their department.
Solution:

sql:
SELECT employee_id, name
FROM employees e
WHERE salary > (SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id);
4. How can you retrieve departments with no employees?
Solution:

sql:
SELECT department_name
FROM departments
WHERE department_id NOT IN (SELECT department_id FROM employees);
5. Write a query to find the employees who work in the same department as 'John'.
Solution:

sql:
SELECT employee_id, name
FROM employees
WHERE department_id = (SELECT department_id
FROM employees
WHERE name = 'John');
6. Find the names of employees who do not have a manager.
Solution:

sql:
SELECT name
FROM employees
WHERE manager_id IS NULL;

7. How can you find employees hired in the same year as another specific employee, say
'Alice'?
Solution:

sql:
SELECT name
FROM employees
WHERE YEAR(hire_date) = (SELECT YEAR(hire_date)
FROM employees
WHERE name = 'Alice');
8. Write a query to find the departments that have more than 5 employees.
Solution:

sql:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;

9. Fetch employee details who have the highest salary in their respective department.
Solution:

sql:
SELECT employee_id, name, salary
FROM employees e
WHERE salary = (SELECT MAX(salary)
FROM employees
WHERE department_id = e.department_id);
10. How can you get the details of employees whose salary is more than the maximum salary of
department 10?
Solution:

sql:
SELECT employee_id, name
FROM employees
WHERE salary > (SELECT MAX(salary) FROM employees WHERE department_id = 10);
11. Retrieve the product names that have never been ordered.

Solution:

sql:
SELECT product_name
FROM products
WHERE product_id NOT IN (SELECT product_id FROM orders);
12. Find employees whose salary is higher than their manager’s salary.

Solution:

sql:
SELECT e.employee_id, e.name
FROM employees e
WHERE e.salary > (SELECT salary
FROM employees
WHERE employee_id = e.manager_id);
13. Write a query to get the list of employees who have worked on all projects.

Solution:

sql:
SELECT employee_id
FROM employees
WHERE employee_id NOT IN
(SELECT employee_id
FROM projects
WHERE project_id NOT IN (SELECT project_id FROM employees_projects));

14. Retrieve customers who placed an order in 2023 but not in 2024.

Solution:

sql:
SELECT customer_id
FROM orders
WHERE YEAR(order_date) = 2023
AND customer_id NOT IN (SELECT customer_id
FROM orders
WHERE YEAR(order_date) = 2024);
15. Find employees who do not work in any department.
Solution:

sql:
SELECT employee_id, name
FROM employees
WHERE department_id IS NULL;

You might also like