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.

25 min 3 practice tasks
SQL JOINs | INNER vs LEFT vs RIGHT vs FULL OUTER | Clearly Explained | Beginners Start Here! by Maven Analytics on YouTube

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 orders columns 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.

1

Orders per enterprise customer

Account 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?
  1. An inner join drops customers with no orders. Use LEFT JOIN so every customer stays.
  2. COUNT(*) counts the customer's own row even with no match, giving 1. COUNT(o.id) skips NULLs and gives 0.
SQL · SQLite
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)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
2

Customers who never ordered

The 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?
  1. With an inner join, o.id is never NULL, so the starter returns nothing.
  2. The anti-join pattern: LEFT JOIN the other table, then keep rows where its key IS NULL.
SQL · SQLite
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)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
3

Orders by channel, including unattributed

Break 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?
  1. The inner join silently drops every order whose campaign_id is NULL. Switch to LEFT JOIN.
  2. Replace the NULL channel with a label: COALESCE(c.channel, 'unattributed'), and group by the same expression.
SQL · SQLite
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)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue