Learn SQL
-- joining.outer_joins

LEFT / RIGHT / FULL OUTER JOIN in SQL

Keep the orphans — NULL-fill what has no match

--concept

INNER keeps only matching pairs. LEFT keeps every left row, NULL-filling missing right sides; RIGHT mirrors; FULL keeps both sides’ orphans. LEFT is the workhorse (optional relations, orphan-finding via WHERE right.key IS NULL). RIGHT is rare — swap table order instead. FULL audits two-sided completeness.

--how it works
1

INNER is intersection

Only pairs matching on both sides survive. Anything orphaned vanishes.

2

LEFT keeps left + NULLs

Every left row survives; missing right sides become NULL. The optional-relation workhorse.

3

RIGHT/FULL mirror

RIGHT keeps right rows; FULL keeps both sides. Swap tables instead of RIGHT for readability.

4

Orphan-finder pattern

LEFT JOIN … WHERE right.key IS NULL lists left rows with no match — data-quality audit.

--visualization
Grace = Research orphan
Grace survives with NULL location
namedepartmentlocation
AliceEngineeringBerlin
BobSalesMadrid
CynthiaEngineeringBerlin
DavidMarketingLisbon
ElenaSalesMadrid
FaridEngineeringBerlin
GraceResearchNULL
Stage 1 / 1
LEFT JOIN with a Research orphan (no department row): 7 of 7 rows survive.
--example
query.sql
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id WHERE d.dept_id IS NULL;
Result
namelocation

Every employee matches a department here, so the orphan query returns zero rows. Add a Research employee with no department row and she appears with NULL location — exactly the row INNER would silently drop.

--variants

LEFT preserves orphans

Berlin matches fill; others NULL-fill but survive.

query.sql
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id;
Result
namelocation
AliceBerlin
BobMadrid
CynthiaBerlin
DavidLisbon
ElenaMadrid
FaridBerlin
1Each employee probes departments by key.
2Matches glue into wide rows.
3A Research employee with no department row would survive with NULL location.

Orphan audit query

Zero rows here — that emptiness is the healthy signal.

query.sql
1SELECT e.* FROM employees e LEFT JOIN departments d ON e.department = d.dept_id WHERE d.dept_id IS NULL;
1LEFT keeps everyone including orphans.
2WHERE IS NULL keeps only the orphans.
3Empty result means referential health; rows mean missing parents.
--key facts
INNERmatches only — non-matches vanish both sides
LEFT + IS NULLthe orphan-finder to memorize
FULLboth sides’ orphans — two-sided audit
--use cases
Optional relations: users with optional profiles, orders with optional coupons
Completeness audits: orphans, unmapped keys, two-sided FULL reconciliation
--under the hood
Outer joins preserve row counts of the kept side — planners use hash/right-outer shapes; INNER commutes freely, OUTER nesting does not.
Index the nullable-side join key or orphan scans degrade to nested loops over full tables.

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.