SQL LEFT JOIN: Keep Rows That Have No Match
An inner join drops rows that have no partner. Sometimes those are exactly the rows you care about: customers who never ordered, books nobody bought, products with zero reviews. A LEFT JOIN keeps every row from the table on the left side of the join, whether or not it has a match.
How LEFT JOIN fills the gaps
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id returns every customer. Customers with orders appear once per order, just like an inner join. Customers without orders appear once, with every column from orders set to NULL. The table written first is the "left" one, so the order of tables matters here, unlike with an inner join.
Counting including zeros
The first query counts orders per customer. Because it counts o.id rather than *, the NULL row for Lucas Meyer counts as 0. count(*) would count that row and report 1, which is wrong. This combination, LEFT JOIN plus count(right_table.column), is the standard way to produce totals that include zeros.
Expected output
name | orders Ava Brooks | 2 Liam Chen | 2 Emma Rossi | 1 Noah Kim | 1 Sofia Patel | 1 Lucas Meyer | 0 name Lucas Meyer
Finding rows with no match
The second query keeps only the rows where the right side is NULL: WHERE o.id IS NULL. The result is every customer who has never placed an order. This pattern is sometimes called an anti-join. The same question can be asked with NOT EXISTS, shown in the subqueries chapter.
WHERE versus ON
Conditions on the right table behave differently depending on where you put them. In ON o.customer_id = c.id AND o.ordered_on >= '2025-10-01', customers without recent orders still appear, with NULLs. Move that date test into WHERE and those customers disappear, because NULL >= '2025-10-01' is not true. Put filters on the right-hand table in ON when you want to keep unmatched rows.
Other outer joins
RIGHT JOIN keeps every row from the right table and FULL OUTER JOIN keeps unmatched rows from both. SQLite supports both since version 3.39, but a RIGHT JOIN can always be rewritten as a LEFT JOIN with the tables swapped, which most people find easier to read.
Common mistakes
- Using
count(*)with a left join and counting the empty row as 1. - Filtering the right table in
WHEREand accidentally turning the query back into an inner join. - Forgetting which table is "left": it is the one in
FROM, beforeLEFT JOIN.
Summary
LEFT JOINkeeps every left-table row; missing right-side columns areNULL.- Count a right-table column to get zeros for rows without matches.
WHERE right.id IS NULLfinds rows with no match.- Right-table filters belong in
ONif unmatched rows should stay.
Quiz: LEFT JOIN