LEFT / RIGHT / FULL OUTER JOIN in SQL
Keep the orphans — NULL-fill what has no match
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.
INNER is intersection
Only pairs matching on both sides survive. Anything orphaned vanishes.
LEFT keeps left + NULLs
Every left row survives; missing right sides become NULL. The optional-relation workhorse.
RIGHT/FULL mirror
RIGHT keeps right rows; FULL keeps both sides. Swap tables instead of RIGHT for readability.
Orphan-finder pattern
LEFT JOIN … WHERE right.key IS NULL lists left rows with no match — data-quality audit.
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id WHERE d.dept_id IS NULL;
| name | location |
|---|
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.
LEFT preserves orphans
Berlin matches fill; others NULL-fill but survive.
1SELECT e.name, d.location FROM employees e LEFT JOIN departments d ON e.department = d.dept_id;
| name | location |
|---|---|
| Alice | Berlin |
| Bob | Madrid |
| Cynthia | Berlin |
| David | Lisbon |
| Elena | Madrid |
| Farid | Berlin |
Orphan audit query
Zero rows here — that emptiness is the healthy signal.
1SELECT e.* FROM employees e LEFT JOIN departments d ON e.department = d.dept_id WHERE d.dept_id IS NULL;
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.