Before you start, you need
Match rows with INNER JOIN · Work correctly with NULL values
Keep the left table even when a match is missing
You want to review every customer, including those who have never bought anything. LEFT JOIN keeps the rows from the left table, even when it finds no related data on the right.
The left table is the one you write before LEFT JOIN, that is, the one in FROM. In customers LEFT JOIN orders, a customer with no orders still appears. The order columns show NULL, because there is no matching row.
A customer with several orders appears once next to each one. So keeping every customer does not mean showing one row per customer. The difference from INNER JOIN lies in what happens when there is no match.
If you want every customer but only their shipped orders, add the status condition to ON. That decides which orders can match. If you put it in WHERE, the rows without an order are removed, because their status is NULL, and so are the rows of orders that have not been shipped.
SELECT ...
FROM left AS a
LEFT JOIN right AS b ON a.id = b.reference;See it in action
Let's look at Amy Wilson and Peter Hill: how do their orders show up, including the case where there are none?
| id | name |
|---|---|
| 1 | Amy Wilson |
| 6 | Peter Hill |
| id | customer_id |
|---|---|
| 101 | 1 |
| 103 | 1 |
| 111 | 1 |
Before running: If a customer has no orders, does their name disappear, or does it appear with a missing order_id?
SELECT c.name, p.id AS order_id
FROM customers AS c
LEFT JOIN orders AS p
ON c.id = p.customer_id
WHERE c.id IN (1, 6)
ORDER BY c.id, p.id;| name | order_id |
|---|---|
| Amy Wilson | 101 |
| Amy Wilson | 103 |
| Amy Wilson | 111 |
| Peter Hill | NULL |
- The filter
c.id IN (1, 6)picks Amy and Peter from the left table. LEFT JOIN keeps both, even though one of them has no orders. - Amy appears next to each of her orders. Peter appears with
order_idset to NULL: his name is still there even though no order matches. - To look only for shipped orders, add
p.status = 'shipped'to ON. If you put that right-side condition in WHERE, Peter's row and the rows for Amy's orders that have not been shipped would disappear.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Keep customers without orders
Write a query that returns every customer and the ids of their orders, including customers with no orders.
Expected result: 20 rows with the columns name, order_id, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Every customer must be kept even when there is no related order.
Match c.id with p.customer_id.
Select c.name and p.id AS order_id.
Solution explained
SELECT c.name, p.id AS order_id FROM customers AS c LEFT JOIN orders AS p ON c.id = p.customer_id;Peter Hill and Miles Lee appear with order_id NULL because they have no orders.
See what changes with WHERE
Guided practice
See what changes with WHERE
Show name and order_id using LEFT JOIN, but apply p.status = 'shipped' in WHERE to compare the effect.
Expected result: 9 rows with the columns name, order_id, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
What happens to a customer with no orders if you require p.status = 'shipped' in WHERE?
Use the same LEFT JOIN as in the previous activity, but put the status condition in WHERE instead of ON.
Compare the row count with the previous activity: customers with no shipped orders disappear because their status is NULL.
Solution explained
SELECT c.name, p.id AS order_id FROM customers AS c LEFT JOIN orders AS p ON c.id = p.customer_id WHERE p.status = 'shipped';This output drops customers with no shipped orders. It is a deliberate comparison and does not answer the previous question.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Keep even the gaps
Show the customer name and p.id as order_id for every customer. Match only their shipped orders, inside ON.
Expected result: 14 rows with the columns name, order_id, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The table that must keep all its rows goes on the left.
The filter on the right-hand table must be part of the match.
Add p.status = 'shipped' to ON, joined with AND.
Solution explained
SELECT c.name, p.id AS order_id FROM customers AS c LEFT JOIN orders AS p ON c.id = p.customer_id AND p.status = 'shipped';The filter inside ON limits the matches without dropping any customers.
Key takeaways
- LEFT JOIN keeps the rows on the left.
- A missing match produces NULL on the right.
- A filter in ON limits the matches; in WHERE it can remove rows.