Intermediate · Lesson 8 of 20

Subqueries with IN

Subqueries work great with IN for matching multiple values.

Find employees in high-paying departments:

SELECT * FROM employees
WHERE department_id IN (
    SELECT department_id FROM employees
    GROUP BY department_id
    HAVING AVG(salary) > 80000
);

The subquery returns a list of department IDs, then IN checks each employee.

NOT IN finds employees NOT in those departments.

Check your understanding

Find employees in departments where average salary exceeds $80,000.

Examples run on the employees sample database. Open any of them in the playground to experiment.