TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 11 of 14 · By Ethan Cole · Updated November 21, 2026

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.

SQLite · editable

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 WHERE and accidentally turning the query back into an inner join.
  • Forgetting which table is "left": it is the one in FROM, before LEFT JOIN.

Summary

  • LEFT JOIN keeps every left-table row; missing right-side columns are NULL.
  • Count a right-table column to get zeros for rows without matches.
  • WHERE right.id IS NULL finds rows with no match.
  • Right-table filters belong in ON if unmatched rows should stay.

Quiz: LEFT JOIN

1. A customer has no orders. In customers LEFT JOIN orders, what are their order columns?

Left joins fill missing right-side columns with NULL.

2. Which expression counts orders per customer and gives 0 for customers without orders?

count(column) ignores the NULL produced for unmatched rows.

3. How do you find customers who never ordered?

Unmatched rows have NULL on the right side; test it with IS NULL.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.