Relational Joins & Preservation Semantics
Pair tuples across relations using INNER, LEFT, RIGHT, FULL, SELF, CROSS, and anti-join idioms while tracking preserved vs unmatched rows.
JOIN constructs a combined tuple space before WHERE runs; moving a right-table filter from ON to WHERE silently turns a LEFT JOIN into an INNER JOIN.Module 03 · Joins · Reading Room
Mental Model
- 01.
Think of a join as matching candidates from two sets across a predicate gate: matching pairs fuse into wide rows.
- 02.
LEFT / RIGHT / FULL OUTER joins rescue unmatched rows by padding the missing side with NULLs after ON evaluation.
- 03.
Anti-joins use LEFT JOIN followed by WHERE right.key IS NULL to surface orphans — users without orders.
- 04.
Self-joins attach two aliases to the same table so peer rows (same department, paired teammates) can meet.
Interactive Module Lab · Joins
13. LEFT JOIN (Users & Orders) — sqlSELECT users.name, orders.product_name, orders.amount, orders.statusFROM usersLEFT JOIN orders ON users.id = orders.user_idORDER BY orders.amount DESC;
SOURCE: USERS
Scanned 10 rows from table 'users'.
| # | id | name | department_id | role | salary | state |
|---|---|---|---|---|---|---|
| 1 | 1 | Alice Smith | 1 | Software Engineer | 95,000 | keep |
| 2 | 2 | Bob Jones | 2 | Product Manager | 110,000 | keep |
| 3 | 3 | Charlie Lee | 1 | Software Engineer | 88,000 | keep |
| 4 | 4 | Diana Prince | 3 | UX Designer | 76,000 | keep |
| 5 | 5 | Evan Wright | 2 | Data Analyst | 82,000 | keep |
| 6 | 6 | Fiona Gallagher | 1 | Tech Lead | 135,000 | keep |
| 7 | 7 | George Clark | NULL | Intern | 42,000 | keep |
| 8 | 8 | Hannah Abbott | 2 | Scrum Master | 89,000 | keep |
Worked Presets in This Module
Each user is paired with the department row whose id matches. Rows without a partner are dropped.
Every user is kept. Users with no orders get NULLs on the order side instead of disappearing.
Marketing has no employees, but a RIGHT JOIN keeps every department and fills the user side with NULL.
Unmatched rows from either table survive: George (no department) and Marketing (no staff).
LEFT JOIN, then keep only the rows where the right side is NULL. A classic way to find "missing" things.
Joins run left to right. The result of the first join becomes the input of the second.