Skip to content
CodeItRaw
Path

Queries inside queries: returning one value, filtering with IN and EXISTS, and looking at the outer row.

Key ideas

  • A subquery returning one value can be compared like a constant.
  • IN (...) checks a list, EXISTS (...) asks "is there at least one row?".
  • A correlated subquery uses the outer row's values and runs again for each row.

Pattern

SELECT name
FROM employees e
WHERE salary > (
  SELECT AVG(salary) FROM employees x
  WHERE x.dept_id = e.dept_id
);

Finish these first:SQL: Joins

Lessons

Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.

  1. 01

    A query inside a query

    0/2 tasks

  2. 02

    EXISTS, and the subquery that looks at the outer row

    0/2 tasks

Boss

  • 03

    Above the department average

    SQL query

  • The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.