Learn SQL
-- joining.on_vs_where

ON vs WHERE in OUTER JOINs in SQL

The silent-INNER bug every review must catch

--concept

ON decides matching during the join — non-matching left rows survive a LEFT JOIN with NULLs. WHERE filters after — testing the nullable side kills the NULL rows, silently converting LEFT into INNER. Rule: match conditions in ON; keep outer-ness by testing nullable columns only for IS NULL or moving filters into ON.

--how it works
1

ON matches during

Decides pairing inside the join. Unmatched LEFT rows survive as NULLs.

2

WHERE filters after

Tests the joined result. NULL-side tests fail — orphans vanish.

3

Silent-INNER mechanism

WHERE d.x = 1 after LEFT JOIN kills NULLs — LEFT behaves as INNER with no error.

4

Placement rule

Optional-side predicates in ON; IS NULL orphan checks stay in WHERE.

--visualization
ON preserves outer rows (NULL-fill)
namelocation
AliceBerlin
BobNULL
CynthiaBerlin
DavidNULL
ElenaNULL
FaridBerlin
Stage 1 / 1
Six in, six out — NULLs survive.
--example
query.sql
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id AND d.location = 'Berlin';
Result
namelocation
AliceBerlin
Bobnull
CynthiaBerlin
Davidnull
Elenanull
FaridBerlin

Berlin matches survive with location filled; everyone else survives with NULL. Move the location test to WHERE and the NULL rows vanish — the LEFT silently behaves as INNER.

--variants

Filter in ON — stays LEFT

Berlin matches fill, others NULL-fill and survive.

query.sql
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id AND d.location = 'Berlin';
Result
namelocation
AliceBerlin
Bobnull
CynthiaBerlin
Davidnull
Elenanull
FaridBerlin
1Join pairs only where department matches AND location is Berlin.
2Non-Berlin employees survive with NULLs — outer-ness kept.
3Six rows in, six rows out.

Filter in WHERE — becomes INNER

Same predicate, different placement, fewer rows.

query.sql
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id WHERE d.location = 'Berlin';
Result
namelocation
AliceBerlin
CynthiaBerlin
FaridBerlin
1Join first preserves all six with NULLs.
2WHERE then drops every NULL-location row.
3Three rows survive — silently INNER. The review catch.
--key facts
ONmatching-time — preserves outer rows
WHEREfilter-after — kills NULLs, makes INNER
Review ruleWHERE on nullable side after LEFT = bug
--use cases
Filtered optionals: Berlin departments with all employees listed
PR review gate: WHERE on nullable side after LEFT JOIN
--under the hood
Planners cannot push WHERE on the nullable side into the outer input without changing semantics — placement is semantic, not cosmetic.
IS NULL checks are the exception the optimizer recognizes as anti-join.

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.