You are on page 1of 2

Sub queries exercise

Consider the following employees and departments tables from the sample
database:

1. Find all employees who locate in the location with the id 1700.
SELECT employee_id, first_name, last_name FROM employees
WHERE department_id IN (SELECT department_id FROM
departments WHERE location_id = 1700) ORDER BY first_name ,
last_name;
2.  find all employees who do not locate at the location 1700:
SELECT employee_id, first_name, last_name FROM employees
WHERE department_id NOT IN (SELECT department_id FROM
departments WHERE location_id = 1700) ORDER BY first_name ,
last_name;

3. finds the employee id, first_name, last _name who have the highest
salary:
SELECT employee_id, first_name, last_name, salary FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees)
ORDER BY first_name , last_name;
4. finds all employees who salaries are greater than the average salary of all
employees:
SELECT
employee_id, first_name, last_name, salary FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

5. finds all departments which have at least one employee with the salary is
greater than 10,000.
SELECT department_name FROM departments d WHERE
EXISTS( SELECT FROM employees WHERE salary > 10000 AND
e.department_id = d.department_id);

6. finds all departments that do not have any employee with the salary
greater than 10,000:
SELECT department_name FROM departments d WHERE NOT
EXISTS( SELECT 1 FROM employees e WHERE salary > 10000
AND e.department_id = d.department_id);

7. finds all employees whose salaries are greater than the lowest salary of
every department:
SELECT employee_id, first_name, last_name, salary
FROM employees WHERE salary >= ALL (SELECT MIN(salary)
FROM employee GROUP BY department_id)
ORDER BY first_name , last_name;
8. Finds all employees whose salaries are greater than or equal to the highest
salary of any department.
SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary >= ANY (SELECT
MAX(salary)
FROM
employees
GROUP BY department_id);

You might also like