LEFT, RIGHT & FULL JOIN
Three users, but Cleo never ordered, so an inner join drops her. Keep her anyway: pin all three users into the result and let the order columns arrive empty wherever no user_id lines up.
- ▸Keep the matched pairs an inner join makes, then add back every row that matched nothing, padded with NULL.
- ▸A padded NULL marks no match, so
orders.id IS NULLfinds who never ordered. - ▸Move one filter from ON to WHERE and the kept rows silently vanish, the join narrowing to only the matches.
Cleo is user 3, and the inner join from the last topic dropped her: no order carries user_id 3, so she found no partner and vanished. Often that is the wrong answer. A report of every user and their spending has to list Cleo too, showing nothing, not omit her.
SELECT name, amount FROM users LEFT JOIN orders ON users.id = orders.user_id does exactly that. The engine still forms the matched pairs an inner join would: Ada with her two orders, Brent with his one. Then it does one more thing. For every left-table row that matched nothing, it adds the row back with the right table's columns filled by NULL. Cleo returns as (Cleo, NULL), her name present, her amount blank because no order paired with her.
The left table is the one named before the words LEFT JOIN, here users, and every one of its rows is guaranteed a place in the result. LEFT OUTER JOIN is the full spelling; the OUTER is optional, so LEFT JOIN and LEFT OUTER JOIN name the same operation.