Learn SQL
-- joining.fanout_semi_anti

Fan-out, Semi-join & Anti-join in SQL

Why SUM double-counts after JOIN — and EXISTS fixes it

--concept

One-to-many matches replicate the one-side row per match — aggregating after fan-out double-counts (SUM over duplicated salaries). Fix by pre-aggregating the many-side in a CTE, or use semi-join (EXISTS/IN — existence without duplication) and anti-join (NOT EXISTS — rows with no match, NULL-safe unlike NOT IN).

--how it works
1

Fan-out replicates

One-to-many matches copy the one-side row per match. SUM after fan-out double-counts.

2

Pre-aggregate first

Roll the many-side to one row per key in a CTE, then join. Canonical fix.

3

Semi-join checks existence

EXISTS returns left rows having a match — never duplicated, short-circuits.

4

Anti-join checks absence

NOT EXISTS returns left rows with no match — NULL-safe. NOT IN is poisoned by NULLs.

--visualization
Semi-join: existence without duplication
dept_idlocation
EngineeringBerlin
SalesMadrid
MarketingLisbon
Stage 1 / 1
Each department returned once — short-circuit probe, no replication.
--example
query.sql
1SELECT * FROM departments d WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id);
Result
dept_idlocationhead
EngineeringBerlinCynthia
SalesMadridElena
MarketingLisbonDavid

Each department asks: does any employee point at me? All three do, each returned exactly once — no duplication even though Engineering matches three employees. A JOIN would have tripled it.

--variants

Semi-join without duplication

Engineering matches thrice but returns once.

query.sql
1SELECT * FROM departments d WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department = d.dept_id);
Result
dept_idlocationhead
EngineeringBerlinCynthia
SalesMadridElena
MarketingLisbonDavid
1Per department, engine probes employees for first match and stops.
2SELECT 1 is idiom — list ignored, existence only.
3No fan-out: three departments, three rows.

Pre-aggregate before join

Fan-out-proof totals.

query.sql
1SELECT d.dept_id, COALESCE(t.total, 0) AS total FROM departments d LEFT JOIN (SELECT department, SUM(salary) AS total FROM employees GROUP BY department) t ON t.department = d.dept_id;
Result
dept_idtotal
Engineering249000
Sales110000
Marketing47000
1Derived table rolls employees to one row per department.
2Join then cannot replicate — keys are unique on the many side.
3COALESCE covers departments with zero staff.
--key facts
Fan-outone-to-many replicates — aggregate-before-join
SemiEXISTS: existence without duplication
AntiNOT EXISTS: NULL-safe absence; NOT IN is poisoned
--use cases
Dashboards: totals that must not double-count after joins
Existence screens: has-orders, missing-profile queries
--under the hood
Semi/anti compile to hashed semi-joins — one probe per outer row, early exit; JOIN+DISTINCT materializes duplicates then dedups — wasteful.
Pre-aggregation enables the planner to pick merge/hash on narrow inputs.

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.