Learn SQL
-- filtering.anti_join_not_exists

EXISTS & Anti-join Patterns in SQL

NULL-immune existence — the safe NOT IN

--concept

EXISTS tests whether a correlated subquery returns any row — TRUE/FALSE, never UNKNOWN, immune to NULL poisoning, short-circuits on first match, ignores the SELECT list (SELECT 1 idiom). NOT EXISTS is the safe anti-join (employees without departments). LEFT JOIN … WHERE right.key IS NULL is equivalent and sometimes planner-friendlier. NOT IN is concise but poisoned: one NULL returns zero rows.

--how it works
1

EXISTS is TRUE/FALSE

Correlated probe returning any row. Never UNKNOWN — NULL-immune. Short-circuits first hit.

2

NOT EXISTS anti-joins

Left rows with no match. The safe absence test.

3

LEFT/IS NULL mirrors

LEFT JOIN … WHERE right.key IS NULL — visible, sometimes planner-friendlier.

4

NOT IN poisons

One NULL in the set → zero rows, silent. Prefer NOT EXISTS; filter NULLs if forced.

--visualization
Anti-join empty = healthy — NULL-immune, short-circuits
orphans
Stage 1 / 1
EXISTS: TRUE/FALSE only, SELECT 1 idiom, stops at first hit.
--example
query.sql
1SELECT e.name FROM employees e WHERE NOT EXISTS (SELECT 1 FROM departments d WHERE d.dept_id = e.department);
Result
name

Every employee matches a department, so the anti-join is empty — healthy signal. Add a Research orphan and she alone appears. NOT IN with a NULL in the set would have wrongly emptied even healthy matches.

--variants

Departments that HAVE staff

Existence without duplication.

query.sql
1SELECT * FROM departments d WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id);
Result
dept_idlocation
EngineeringBerlin
SalesMadrid
MarketingLisbon
1Per department, probe stops at first employee.
2SELECT 1 idiom — list ignored.
3JOIN would triple Engineering; EXISTS returns once.

Staff without departments (safe)

Empty here = healthy. NULL-proof.

query.sql
1SELECT e.name FROM employees e WHERE NOT EXISTS (SELECT 1 FROM departments d WHERE d.dept_id = e.department);
2-- unsafe twin: WHERE department NOT IN (SELECT dept_id FROM departments); -- NULL poisons
1Anti-join lists orphans only.
2NOT IN twin returns nothing if subquery holds any NULL.
3Default NOT EXISTS in production code.
--key facts
EXISTSshort-circuit, NULL-immune, ignores list
NOT EXISTSsafe anti-join headline
NOT IN trapone NULL → zero rows, silent
--use cases
Orphan audits + missing-match screens
Existence checks without COUNT(*) > 0 scans
--under the hood
Semi/anti compile to hashed semi-joins — early exit per outer row; COUNT answers how-many (full scan), not whether.
SELECT 1 vs SELECT * inside EXISTS plans identically — list never evaluated.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.