Joins, drawn row by row
Venn diagrams make joins look like set overlaps, then a join returns more rows than either table has. Draw a join instead as rows from one table matched against rows from the other, and inner, left and full joins each become one simple rule.
01 Two small tables
Three users and four orders. Each order stores the id of the user who placed it. Ben has placed nothing, and the mug belongs to user 4, who has been deleted.
Every join below has the same condition, ON o.user_id = u.id. Only the join type changes, and with it the rule for rows that match nothing.
02 Inner join: only the pairs that match
The plainest way to picture it: take each user in turn, compare them with every order, and output one row for each pair where the condition is true. JOIN on its own means INNER JOIN.
- this user
- ids match
- ids differ
- new result row
- not reached yet
Nothing compared yet. The result starts empty.
The rule is: a row comes out for every matching pair, and only for those. That is why Ana appears twice and Ben and the mug not at all. A join can return more rows than either table when one row matches many.
03 Left join: keep every row on the left
- this user
- ids match
- new result row
- not reached yet
Same tables, now users LEFT JOIN orders. Users is the left table: each of its rows must come out at least once.
The rule: the matching pairs, plus each left row that matched nothing, once, padded with `NULL`. Use it for “every user and their orders, if any”. To find the users with no orders at all, add WHERE o.user_id IS NULL: only the padded rows survive. A RIGHT JOIN is the same with the sides swapped; most people just swap the tables and write a left join.
04 Full join: keep unmatched rows on both sides
- this user
- ids match
- new result row
- not reached yet
users FULL JOIN orders keeps unmatched rows from both sides. To do that it must remember which orders were matched; the tick marks them.
Full joins are handy for comparing two lists, such as yesterday’s export and today’s: the NULL side tells you which rows are only in one of them. Postgres, SQL Server and Oracle support FULL JOIN; MySQL does not, so there you combine a left join and a right join with UNION.
05 Same tables, three answers
NULL for the order.NULL for the user.With no condition at all, CROSS JOIN outputs every pair: 3 × 4 = 12 rows. Every other join is that list of pairs, filtered by ON, with the unmatched rows added back according to the join type. The order the rows come out in is not guaranteed; add ORDER BY when it matters.
06 How the database really finds the pairs
Trying every pair is the meaning of a join, not always how it is computed. For big tables, the planner picks one of three methods. All three give the same rows; they differ in work.
Run EXPLAIN in front of the inner join and Postgres shows the method it chose, for example Hash Join with Hash Cond: (o.user_id = u.id).
07 When it bites
The most common join bug: a WHERE condition on the right table quietly turns a left join back into an inner join. WHERE runs after the join, and the padded rows have NULL there, which never equals anything.
SELECT u.name, o.itemFROM users uLEFT JOIN orders o ON o.user_id = u.idWHERE o.item = 'book';-- Ana | book
SELECT u.name, o.itemFROM users uLEFT JOIN orders o ON o.user_id = u.idAND o.item = 'book';-- Ana | book, Ben | NULL, Cho | NULL
SELECT u.name, count(*) ... LEFT JOIN ... GROUP BY u.nameCounting the padded rowBen’s NULL row is still a row, so count(*) says 1. Use count(o.item), which skips NULLs, and Ben gets 0.ON o.user_id = u.id -- user_id IS NULLNULL keys never matchNULL = NULL is not true in SQL, so an order with no user_id never joins to anything, not even another NULL.