Section 3 · Lesson 2
LEFT JOIN and finding missing matches
Keep every row from your main table with LEFT JOIN, count zeros correctly, and use the anti-join pattern to find what's missing.
An inner join answers “show me things that match”. Just as often, a business needs “show me
everything, with details where they exist” or even “show me what’s missing”: customers
who never bought, campaigns with no sales, orders no campaign can take credit for. That’s what
LEFT JOIN is for.
LEFT JOIN keeps every row from the left table
SELECT cu.id, cu.first_name, o.id AS order_id, o.amount
FROM customers AS cu
LEFT JOIN orders AS o ON o.customer_id = cu.id;
The table written first (after FROM) is the left table. Every one of its rows appears at
least once:
- If a customer has three orders, you get three rows, just like an inner join.
- If a customer has no orders, you still get one row, with the
orderscolumns filled with NULL.
| Join | Customers with orders | Customers without orders |
|---|---|---|
INNER JOIN |
one row per order | dropped |
LEFT JOIN |
one row per order | one row, order columns NULL |
So the question to ask before every join is: if there’s no match, do I want to lose the row?
Counting with LEFT JOIN: COUNT(*) vs COUNT(column)
SELECT cu.id, COUNT(*) AS wrong, COUNT(o.id) AS right_count
FROM customers AS cu
LEFT JOIN orders AS o ON o.customer_id = cu.id
GROUP BY cu.id;
For a customer with no orders, the join produces one row with NULLs, so COUNT(*) says 1.
COUNT(o.id) ignores NULLs and correctly says 0. When counting the right-hand table after a
left join, always count one of its columns, ideally its primary key.
The anti-join: finding what’s missing
Rows with no match are exactly the rows where the right table’s key came back NULL. Filter on that and you’ve found what’s missing:
-- Advertisers that have no campaigns at all
SELECT a.id, a.name
FROM advertisers AS a
LEFT JOIN campaigns AS c ON c.advertiser_id = a.id
WHERE c.id IS NULL;
(In our data every advertiser runs campaigns, so this one returns nothing, which is itself a useful check.) Test the right table’s primary key or another column that’s never NULL in real rows; that way a NULL can only mean “no match”.
The same idea can be written with NOT EXISTS, which some people find more readable:
SELECT a.id, a.name
FROM advertisers AS a
WHERE NOT EXISTS (SELECT 1 FROM campaigns AS c WHERE c.advertiser_id = a.id);
The WHERE trap
Filters on the right table belong in the ON clause if you want to keep unmatched rows:
-- Customers and their REFUNDED orders, keeping customers with none
FROM customers AS cu
LEFT JOIN orders AS o ON o.customer_id = cu.id AND o.status = 'refunded'
-- Looks similar, but turns into an inner join:
FROM customers AS cu
LEFT JOIN orders AS o ON o.customer_id = cu.id
WHERE o.status = 'refunded'
In the second version, customers without a refunded order have o.status = NULL, and
WHERE throws those rows away. Filters on the left table (like cu.segment = 'enterprise')
are fine in WHERE.
Labels for the gaps
Use COALESCE to turn the NULLs of a left join into something readable, such as
COALESCE(c.channel, 'unattributed') for orders with no campaign.
RIGHT and FULL joins
A RIGHT JOIN is a left join written backwards (keep every row from the second table), and a
FULL OUTER JOIN keeps unmatched rows from both sides. SQLite supports both since version 3.39,
so the video’s examples run here. In practice most analysts stick to LEFT JOIN and simply put
the table they want to keep first.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Orders per enterprise customer
DoneAccount managers want the order count for every customer in the enterprise segment, including customers who have never ordered (they should show 0). Return id, first_name, last_name and the number of orders (any status).
Need a hint?
- An inner join drops customers with no orders. Use
LEFT JOINso every customer stays. COUNT(*)counts the customer's own row even with no match, giving 1.COUNT(o.id)skips NULLs and gives 0.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT cu.id, cu.first_name, cu.last_name, COUNT(*) AS orders
FROM customers AS cu
JOIN orders AS o ON o.customer_id = cu.id
WHERE cu.segment = 'enterprise'
GROUP BY cu.id, cu.first_name, cu.last_name;
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)Customers who never ordered
DoneThe CRM team wants to re-engage customers who signed up but have never placed an order (of any status). Return their id, first_name, last_name and signup_date.
Need a hint?
- With an inner join,
o.idis never NULL, so the starter returns nothing. - The anti-join pattern:
LEFT JOINthe other table, then keep rows where its keyIS NULL.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT cu.id, cu.first_name, cu.last_name, cu.signup_date
FROM customers AS cu
JOIN orders AS o ON o.customer_id = cu.id
WHERE o.id IS NULL;
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)Orders by channel, including unattributed
DoneBreak down all orders by the channel of the campaign they're attributed to. Orders with no campaign (campaign_id is NULL) must appear in their own row labeled unattributed. Return the channel (or unattributed), the number of orders, and the total amount rounded to 2 decimals. Sort by number of orders, highest first.
Need a hint?
- The inner join silently drops every order whose
campaign_idis NULL. Switch toLEFT JOIN. - Replace the NULL channel with a label:
COALESCE(c.channel, 'unattributed'), and group by the same expression.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT c.channel, COUNT(*) AS orders, ROUND(SUM(o.amount), 2) AS total_amount
FROM orders AS o
JOIN campaigns AS c ON c.id = o.campaign_id
GROUP BY c.channel
ORDER BY orders DESC;
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)