LEFT JOIN: keeping rows without a match
Keep every row from one table with SQL LEFT JOIN, find customers with no orders and products never sold, and count correctly so that zero shows as 0.
🎯 By the end of this lesson you will be able to:
- Explain the difference between INNER JOIN and LEFT JOIN
- Find rows with no match using LEFT JOIN and IS NULL
- Count related rows so that zero appears as 0
Outline
What you'll learn and why it matters
Some of the most valuable business questions are about what is missing: customers who signed up but never bought anything, products nobody orders, students who have not submitted work. An inner join can never show these, because it drops rows without a match. A LEFT JOIN keeps them.
Keeping every row from the left table
LEFT JOIN keeps every row from the first (left) table. Where there is a matching row in the right table, its columns are filled in; where there is none, they are NULL.
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id IN (1, 7, 10)
ORDER BY c.customer_id, o.order_id;Result: 5 rows
| name | order_id |
|---|---|
| Asha Patel | 1 |
| Asha Patel | 2 |
| Asha Patel | 12 |
| Sara Lopez | NULL |
| Kofi Mensah | NULL |
| Join | Keeps | Sara (no orders) |
|---|---|---|
JOIN (inner) | Only rows that match in both tables | Not shown |
LEFT JOIN | Every row from the left table, matched or not | Shown once, with NULL order_id |
Finding rows with no match
Combine a LEFT JOIN with IS NULL on the right table's key to keep only the unmatched rows. Who has never ordered?
SELECT c.name, c.email
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;Result: 2 rows
| name | |
|---|---|
| Sara Lopez | NULL |
| Kofi Mensah | kofi@example.com |
Which products have never been ordered?
SELECT p.name
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.product_id
WHERE o.order_id IS NULL;Result: 1 row
| name |
|---|
| Coffee Mug |
This “left join, then IS NULL” pattern is worth memorising. You will use it all the time.
Counting with zero
To count orders per customer including customers with none, left join and count a column from the right table:
SELECT c.name, COUNT(o.order_id) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY orders DESC, c.name;Result: 10 rows
| name | orders |
|---|---|
| Asha Patel | 3 |
| Arjun Singh | 2 |
| Fatima Khan | 2 |
| John Smith | 2 |
| Meera Nair | 2 |
| Ravi Kumar | 2 |
| Emma Brown | 1 |
| Li Wei | 1 |
| Kofi Mensah | 0 |
| Sara Lopez | 0 |
SELECT c.name, COUNT(*) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id IN (7, 10)
GROUP BY c.customer_id, c.name;Result: 2 rows
| name | orders |
|---|---|
| Sara Lopez | 1 |
| Kofi Mensah | 1 |
Practice
Guided exercise
Orders per category, including empty ones
Show each product category and how many orders it has had, using a LEFT JOIN from products so that every category appears. Sort by orders, highest first, then by category.
- Start
FROM products AS pandLEFT JOIN orders AS o ON o.product_id = p.product_id. - Group by
p.category. - Count
o.order_id, not*.
Show the solution
SELECT p.category, COUNT(o.order_id) AS orders
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.product_id
GROUP BY p.category
ORDER BY orders DESC, p.category;Result: 4 rows
| category | orders |
|---|---|
| Books | 5 |
| Electronics | 4 |
| Stationery | 4 |
| Home | 2 |
How to get there: Home has only 2 orders, both for the Water Bottle; the Coffee Mug contributes a joined row with NULL, which COUNT(o.order_id) correctly ignores.
Your turn
Win-back list
Marketing wants to email customers who have never ordered, but only those with an email address. List their name, email and join date.
Show the solution
SELECT c.name, c.email, c.joined_on
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL AND c.email IS NOT NULL;Result: 1 row
| name | joined_on | |
|---|---|---|
| Kofi Mensah | kofi@example.com | 2025-09-14 |
How to get there: The LEFT JOIN plus o.order_id IS NULL finds non-buyers; c.email IS NOT NULL removes Sara, who cannot be emailed.
Common mistakes
Tables in the wrong order
You see: The rows you wanted to keep still disappear
Fix: Put the table whose rows must all appear in FROM, then LEFT JOIN the other.
Using COUNT(*) after a LEFT JOIN
You see: Customers with no orders show 1 instead of 0
Fix: Count a column from the right table: COUNT(o.order_id).
Filtering the right table in WHERE, e.g. WHERE o.status = 'delivered'
You see: The unmatched rows vanish again (their status is NULL)
Fix: Put such conditions in the ON clause: ON o.customer_id = c.customer_id AND o.status = 'delivered'.
Checking IS NULL on a column that can be NULL anyway
You see: Wrong rows reported as missing
Fix: Test the right table's key column, such as o.order_id.
Recap
- LEFT JOIN keeps every row from the left (FROM) table; unmatched right-side columns are NULL.
- LEFT JOIN plus
WHERE right.key IS NULLfinds rows with no match. - Count a right-table column, not
*, so that zero shows as 0. - Conditions on the right table usually belong in ON, not WHERE.
Key terms
- LEFT JOIN
- A join that keeps every row from the left table, filling unmatched columns with NULL.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which query lists customers who have never ordered?
2. After a LEFT JOIN from customers to orders, why does COUNT(*) show 1 for a customer with no orders?
3. In `FROM products LEFT JOIN orders`, which table's rows are all kept?
4. How many rows does a LEFT JOIN from customers to orders give for a customer with 3 orders?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.