IN & EXISTS in SQL
Membership tests against another query’s results
WHERE x IN (SELECT …) asks “is x one of the values that query returned?”; WHERE EXISTS (SELECT …) asks “does that query return any row at all (for this row)?”. Both replace hand-managed id lists with a live query, and both usually optimise to the same semi-join. EXISTS is the safer habit for correlated tests because it short-circuits on the first match.
IN tests membership
WHERE department IN (SELECT dept_id FROM …) — for each outer row, is the value among the subquery’s results?
EXISTS tests existence
WHERE EXISTS (SELECT 1 FROM … WHERE …) — is there any matching row for this outer row? The select list is ignored.
Correlation shapes the choice
Uncorrelated list of known values → IN. Per-row probe that mentions the outer row → EXISTS, which stops at the first match.
Negation: NOT EXISTS is the safe one
NOT IN returns zero rows if the subquery yields a single NULL (three-valued logic). NOT EXISTS has no such trap — default to it.
Engines converge anyway
Modern optimisers rewrite both into semi/anti joins; pick the form that states your intent most clearly.
1SELECT dept_id, location2FROM departments d3WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id AND e.salary > 80000);
| dept_id | location |
|---|---|
| Engineering | Berlin |
For each department the engine asks: does anyone here earn over 80,000? Sales peaks at 58,000 and Marketing at 47,000, so only Engineering survives. The SELECT 1 is convention — EXISTS never looks at the select list.
IN with a subquery
Employees in departments that have a location tracked.
1SELECT name, department2FROM employees3WHERE department IN (SELECT dept_id FROM departments WHERE location IS NOT NULL);
| name | department |
|---|---|
| Alice | Engineering |
| Bob | Sales |
| Cynthia | Engineering |
| David | Marketing |
| Elena | Sales |
| Farid | Engineering |
EXISTS — probe per row
Departments that currently employ someone.
1SELECT d.dept_id, d.location2FROM departments d3WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id);
| dept_id | location |
|---|---|
| Engineering | Berlin |
| Sales | Madrid |
| Marketing | Lisbon |
NOT EXISTS — the anti-join
Departments with no employees at all.
1SELECT d.dept_id2FROM departments d3WHERE NOT EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id);4-- with a Research row present: returns Research
| dept_id |
|---|
| Research |
NOT IN meets NULL
One NULL in the subquery empties the whole result.
1SELECT name FROM employees2WHERE department NOT IN (SELECT dept_id FROM departments WHERE dept_id <> 'Sales' OR 1 = 1);3-- variant: subquery containing NULL4SELECT name FROM employees5WHERE department NOT IN (SELECT NULLIF(dept_id, 'Engineering') FROM departments);
| name |
|---|
| (0 rows) |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.