0% found this document useful (0 votes)
9 views2 pages

SQL Subquery Answers

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)
9 views2 pages

SQL Subquery Answers

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/ 2

SQL Subquery Questions with Answers

(Employees Table)

Employees Table (Assumption)


Columns: employee_id, first_name, last_name, job_id, salary, department_id, manager_id,
hire_date

Easy Level (Basic Subqueries)


1. Find the employees who earn more than the average salary of all employees.
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

2. Display the employees whose salary is equal to the minimum salary in the company.
SELECT * FROM employees WHERE salary = (SELECT MIN(salary) FROM employees);

3. List the employees who were hired after the earliest hire date in the company.
SELECT * FROM employees WHERE hire_date > (SELECT MIN(hire_date) FROM employees);

4. Show employees working in the same department as 'Smith'.


SELECT * FROM employees WHERE department_id = (SELECT department_id FROM employees
WHERE last_name = 'Smith');

5. Find the employee who earns the highest salary using a subquery.
SELECT * FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);

Medium Level (Correlated Subqueries)


6. Display employees who earn more than the average salary of their department.
SELECT * FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE
department_id = e.department_id);

7. Find the employees who have the same job as 'John'.


SELECT * FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE
first_name = 'John');

8. List the employees whose salary is greater than the salary of their manager.
SELECT * FROM employees e WHERE salary > (SELECT salary FROM employees WHERE
employee_id = e.manager_id);

9. Find employees who joined before the manager of department 10.


SELECT * FROM employees WHERE hire_date < (SELECT hire_date FROM employees WHERE
employee_id = (SELECT manager_id FROM employees WHERE department_id = 10 FETCH FIRST
1 ROWS ONLY));

10. Show employees who earn more than the lowest salary in department 30.
SELECT * FROM employees WHERE salary > (SELECT MIN(salary) FROM employees WHERE
department_id = 30);

Hard Level (Nested & Complex Subqueries)


11. Find the second highest salary from the employees table using a subquery.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
12. List the employees who earn the top 3 highest salaries.
SELECT * FROM employees WHERE salary IN (SELECT DISTINCT salary FROM employees ORDER
BY salary DESC FETCH FIRST 3 ROWS ONLY);

13. Display employees whose salary is greater than the average salary of employees hired after
2010.
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE
EXTRACT(YEAR FROM hire_date) > 2010);

14. Find the employees who do not manage anyone (not present as a manager_id in employees
table).
SELECT * FROM employees WHERE employee_id NOT IN (SELECT DISTINCT manager_id FROM
employees WHERE manager_id IS NOT NULL);

15. Display the employees who belong to a department where the total salary is above the
company’s average department salary.
SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM employees
GROUP BY department_id HAVING SUM(salary) > (SELECT AVG(total) FROM (SELECT
SUM(salary) AS total FROM employees GROUP BY department_id)));

You might also like