Module 4 · Relating tables

Keep all rows with LEFT JOIN

By the end you will be able to keep rows even when they have no match.

About 16 min

0 of 52 lessons

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.

Minimal syntax
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?

Sample data: 2 rows
idname
1Amy Wilson
6Peter Hill
Sample data: 3 rows
idcustomer_id
1011
1031
1111

Before running: If a customer has no orders, does their name disappear, or does it appear with a missing order_id?

SQL
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;
Example result: 4 rows
nameorder_id
Amy Wilson101
Amy Wilson103
Amy Wilson111
Peter HillNULL
  1. 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.
  2. Amy appears next to each of her orders. Peter appears with order_id set to NULL: his name is still there even though no order matches.
  3. 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

Not started

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.

See what changes with WHERE

Guided practice

See what changes with WHERE

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Keep even the gaps

Not started

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.

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.

Next: Combine tables and keep control of the result rows