4.4.6Databases

Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

2,236 words10 min readdifficulty · medium1 backlinks

WHY do joins exist?

We'll use two running tables:

Employees

emp_id name dept_id
1 Asha 10
2 Bilal 20
3 Cara NULL

Departments

dept_id dept_name
10 Sales
30 Legal

Note the deliberate mismatches: emp Cara has no department; dept 30 (Legal) has no employee; dept 20 exists in Employees but not in Departments.


The six joins

Figure — Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

HOW joins are built from first principles (Derivation)

Counting check

For our data: INNER has 1 row. Left-unmatched = {Bilal, Cara} = 2. Right-unmatched = {Legal} = 1. LEFT=1+2=3,RIGHT=1+1=2,FULL=1+2+1=4.|\text{LEFT}| = 1+2 = 3,\quad |\text{RIGHT}| = 1+1 = 2,\quad |\text{FULL}| = 1+2+1 = 4.


Worked examples


Common mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine two lists. List A: kids and the lunch-table number they sit at. List B: table numbers and what food is on each table. A join matches each kid to their table's food.

  • INNER: only show kids who sit at a real food-table (skip the kid sitting on the floor and the empty table).
  • LEFT: show every kid — if a kid has no table, write "no food."
  • RIGHT: show every table — even empty ones say "nobody here."
  • FULL: show every kid AND every table, blanks where missing.
  • CROSS: pretend every kid could sit at every table — list all combos.
  • SELF: match kids to other kids at the same table, using the one list twice.

Active recall

INNER JOIN returns which rows?
Only rows matching the ON condition on both tables; unmatched rows on either side are dropped.
LEFT JOIN keeps all rows of which table, and fills what for misses?
All rows of the LEFT table; unmatched RIGHT columns become NULL.
How do you rewrite A RIGHT JOIN B using LEFT?
B LEFT JOIN A (same result, swap order).
FULL OUTER JOIN equals the union of which two joins?
LEFT JOIN ∪ RIGHT JOIN.
A CROSS JOIN of m and n rows produces how many rows?
m × n (Cartesian product), with no ON condition.
Every join can be derived as which sequence of operations?
CROSS JOIN (Cartesian product) → FILTER by ON predicate (gives INNER) → PAD unmatched outer rows with NULL.
What is the standard pattern to find rows with NO match (anti-join)?
LEFT JOIN the other table, then WHERE IS NULL.
Why does a filter on the right table in WHERE turn a LEFT JOIN into an INNER JOIN?
Padded NULL rows fail the comparison (NULL = value is not true), so WHERE removes them after padding.
What clause should hold right-table conditions to preserve LEFT JOIN semantics?
The ON clause (filters before padding), not WHERE (filters after).
What is a SELF JOIN used for?
Joining a table to itself via two aliases — e.g. employee→manager hierarchies or comparing rows within one table.
Why does INNER JOIN ON e.dept_id = d.dept_id drop a row with NULL dept_id?
NULL never equals anything (even NULL), so a NULL key matches no row.
In a self-join pairing, why add a.id < b.id?
To avoid duplicate (A,B & B,A) pairs and self-pairs (A,A).

Connections

  • Relational Algebra — selection σ\sigma, projection π\pi, Cartesian product ×\times underlie joins.
  • Normalization — splits data across tables; joins reassemble it.
  • NULL semantics in SQL — why unmatched outer rows behave the way they do.
  • Indexes — how the optimizer avoids real m×nm\times n work (hash join, merge join, nested loop).
  • Foreign Keys — the relationships joins typically follow.
  • GROUP BY and Aggregation — often combined with joins.

Concept Map

creates need for

reconnect via

core question

drop unmatched

keep all left

keep all right

keep both sides

no condition

two aliases of one table

equivalent to

pads missing with

pads missing with

used for

Normalization splits data

Joins

Key match condition

No match: keep or drop?

INNER JOIN

LEFT JOIN

RIGHT JOIN

FULL OUTER JOIN

CROSS JOIN — m x n

SELF JOIN

NULL values

Hierarchies emp to manager

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, JOIN ka matlab simple hai: do tables ko ek matching condition se jodna. Normalization ki wajah se hum data alag-alag tables mein todte hain (taaki repeat na ho), aur JOIN usi data ko wapas jodta hai. Sabse important sawaal sirf ek hai: jab ek side ki row ka doosri side match nahi milta, tab uska kya karein? Bas yahi pure 6 joins ko define karta hai.

INNER mein sirf wahi rows aati hain jinka dono taraf match ho — baaki gayab. LEFT mein left table ki saari rows rehti hain, right ka match na mile to NULL bhar jata hai. RIGHT ulta hai (right table puri). FULL OUTER mein dono taraf ka sab kuch, jahan match na ho wahan NULL. CROSS mein koi condition hi nahi — har left row har right row ke saath (m × n combinations). SELF mein ek hi table ko do alias se khud se jodte hain, jaise employee aur uska manager same table mein ho.

Andar se har join ek hi formula hai: pehle Cartesian product (saare pairs), phir ON condition se filter (yeh INNER deta hai), phir bachi hui unmatched rows ko NULL se pad karo. Yaad rakho — agar tumhe "no match" wali rows chahiye (anti-join), to LEFT JOIN karo aur WHERE right_key IS NULL lagao.

Ek bada mistake se bacho: LEFT JOIN mein right table ki condition kabhi WHERE mein mat daalo, warna NULL rows hat jaayengi aur tumhara LEFT JOIN chupke se INNER JOIN ban jayega. Right table ki condition hamesha ON clause mein daalo. Yeh ek trick exam aur real coding dono mein paisa banwa deti hai.

Go deeper — visual, from zero

Test yourself — Databases

Connections