How does LEFT JOIN work?
LEFT JOIN keeps every row of the left table; where there is no pair on the right, the right columns are filled with NULL. This NULL means a missing pair, not missing data, so aggregates count the right table's key, for example COUNT(o.id) instead of COUNT(*), and sums are wrapped in COALESCE. The order of tables matters here.
SQL · JOIN
The report "orders per user" says Olha has one order, although she never bought anything.
A manager asks for a report: every user and the number of their orders, including those who have not bought anything yet. The tester takes a LEFT JOIN, all users appear, and Olha has one order. But she has none in the orders table. This lesson explains what LEFT JOIN does to a row with no pair, why a NULL in the result breaks familiar aggregates and how to check such a report.
How it works and why. LEFT JOIN solves the problem of INNER JOIN: rows of the left table must not be lost. For each left row, pairs are searched on the right. If there is a pair, the result is the same as in INNER JOIN. If there is none, the left row still goes into the result once, and all columns of the right table are filled with NULL. The left table becomes the "preserved" side and the right one the "optional" side. So the table order matters here, unlike in INNER JOIN.
The main danger follows. The result contains NULLs that are not in the data: they are created by the join itself. A NULL in a right column means "no pair was found", not "the value is unknown". Any function that reads right-hand columns must take that into account.
Example: Olha with one order. Three users, orders 100 and 101 belong to Anna, 102 to Ivan, Olha has none. The initial query:
SELECT u.email, COUNT(*) AS orders_cnt
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.email;
-- anna 2, ivan 1, olha 1: the last one is wrong
For Olha the result is wrong. Let us take it apart: LEFT JOIN returned one row for her with NULL in the columns o.id and o.total. COUNT(*) counts rows, and there is one row, so it gives 1. COUNT(o.id) counts only non-NULL values, so it gives 0. The fixed query also adds COALESCE for the sum: SUM over only NULLs gives NULL, not 0, and the report would show an empty cell.
SELECT u.email,
COUNT(o.id) AS orders_cnt,
COALESCE(SUM(o.total), 0) AS revenue
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.email;
-- anna 2 / 80, ivan 1 / 20, olha 0 / 0
LEFT JOIN and grouping
| order_id | total | |
|---|---|---|
| anna@test.com | 100 | 50 |
| anna@test.com | 101 | 30 |
| ivan@test.com | 102 | 20 |
| olha@test.com | NULL | NULL |
| COUNT(*) | COUNT(o.id) | |
|---|---|---|
| anna@test.com | 2 | 2 |
| ivan@test.com | 1 | 1 |
| olha@test.com | 1 | 0 |
A diagnostic rule: if a LEFT JOIN report has a group with a count of 1 where 0 was expected, check which column the aggregate counts. It should count a column of the right table, best of all its key, because a key is never NULL in a real row.
Limits and alternatives. The second dangerous case is a chain. If a plain JOIN to the next table follows a LEFT JOIN, the rows with NULL find no pair again and vanish:
-- wrong: an INNER after a LEFT drops users with no orders again
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
JOIN payments p ON p.order_id = o.id
-- right: a LEFT chain, the pair stays optional
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
LEFT JOIN payments p ON p.order_id = o.id
So in a chain where all rows on the left are needed, every next join must be LEFT too. The second trap is a condition on the right table in WHERE (a separate lesson covers ON versus WHERE). RIGHT JOIN does the mirror image, so everything said applies to it with the sides swapped. To check a LEFT JOIN, compare row counts: the result is not smaller than the left table, and exceeds it only when some left row has several pairs. If you only need to show that a related record exists, EXISTS is simpler and does not multiply rows. The behaviour is the same in all databases; only the way clients print NULL differs.
Common mistakes.
COUNT(*)instead ofCOUNT(right_column)after a LEFT JOIN: "orphans" count as one record.SUM/AVGwithoutCOALESCE: a NULL in the report instead of zero, andAVGignores NULLs, so an average over LEFT JOIN rows leaves out users with no orders.- A plain
JOINafter a LEFT JOIN: everything that had no pair vanishes again. - Mixing up the sides: LEFT JOIN preserves the left table, and if all orders are needed, not all users, orders must be on the left.
- Reading a NULL in the result as data: it is only the absence of a pair.
Check yourself
users has 10 rows, orders has 16: each user has 2 orders, except two users with none. How many rows does users LEFT JOIN orders return?
Answer
8 users give 2 rows each (16), the two with no orders give 1 row each with NULL: 18 in total. The result is bigger than users because LEFT JOIN keeps everyone on the left and multiplies by pairs on the right.
Why does AVG(o.total) after a LEFT JOIN per user give NULL, not 0, for Olha who has no orders, and what do you do about it?
Answer
AVG ignores NULL, and all values in Olha's group are NULL, so the average is undefined. If the requirement needs zero, wrap it as COALESCE(AVG(o.total), 0). But an average with no orders is perhaps better left empty: zero is misleading.