Lesson 6 · Milestone: Join + group

Finding rows with no partner

"Customers who never ordered." "Visits with no purchase." "Users who never logged in." These questions look for something that is missing. You can't SELECT a missing row, but a LEFT JOIN turns "missing" into a NULL you can test.

What you will be able to do:

Three words you need

anti-joinKeep the left rows that have no match on the right.
keyThe column the join matches on. In a real right-table row it is never NULL.
NOT EXISTSAnother way to say "no matching row exists". You'll see it in Sprint 4.

1. The recipe

SELECT …
FROM   Left  a
LEFT JOIN Right b ON a.id = b.a_id    -- 1. every left row stays; no partner → b.* is NULL
WHERE  b.a_id IS NULL;                -- 2. keep ONLY the rows that got NULLs

This is the same idea the MySQL manual shows for finding "rows in a table that have no counterpart in another table" (MySQL manual). It works because of the run order: the join builds the combined table first, then WHERE filters it.

1. FROM + LEFT JOIN  →  2. WHERE … IS NULL  →  3. GROUP BY  →  4. SELECT

Click through the steps. The example uses app sessions and purchases, not the exercise tables:

-- For each user, how many sessions had NO purchase?
SELECT s.user_id, COUNT(*) AS empty_sessions
FROM Sessions s
LEFT JOIN Purchases p ON s.session_id = p.session_id
WHERE p.purchase_id IS NULL
GROUP BY s.user_id;
1 · Sessions LEFT JOIN Purchases session user purchase 1       u7   NULL 2       u7   p1 2       u7   p2 3       u8   p3 4       u7   NULL 5       u9   NULL session 2 had 2 purchases → 2 rows 2 · WHERE purchase IS NULL session user 1       u7 4       u7 5       u9 only sessions with no partner 3 · GROUP BY user + COUNT user empty_sessions u7   2 u9   1 u8 is gone: every session of u8 had a purchase

LEFT JOIN keeps every session. Sessions 1, 4 and 5 found no purchase, so their purchase columns are NULL. Session 2 found two purchases, so it appears twice.

WHERE p.purchase_id IS NULL keeps only the rows where the join found nothing. Session 2's duplicate rows disappear, because they had partners.

GROUP BY s.user_id puts the leftover sessions into buckets, and COUNT(*) counts each bucket. u7 has 2 empty sessions and u9 has 1.

2. Three details interviewers check

Interview talking point. The same question can be written with NOT EXISTS (SELECT 1 FROM Purchases p WHERE p.session_id = s.session_id) (MySQL manual). Avoid NOT IN (SELECT session_id …): if that list contains a single NULL, x NOT IN (…, NULL) is never TRUE, and the query returns nothing. That's the same UNKNOWN rule again.

Practice from memory

Try not to scroll up.

1. After the LEFT JOIN, session 2 (two purchases) appears how often?

2. Which WHERE keeps only sessions with no purchase?

3. Why does WHERE p.purchase_id = NULL return zero rows?

4. When does WHERE p.purchase_id IS NULL run?

5. You move p.purchase_id IS NULL from WHERE into the ON clause. What comes back?

Fill in the blanks: Lesson 5's tables

Students(id, name) and Lockers(student_id, locker_no). Return the names of students who have no locker.

SELECT s.name
FROM Students s
 Lockers l ON s.id = l.student_id
 l. ;

🏆 Challenge: LeetCode 1581 (Customer Who Visited but Did Not Make Any Transactions)

Your task: open 1581 and choose MySQL. Tables: Visits(visit_id, customer_id) and Transactions(transaction_id, visit_id, amount). For each customer, count the visits with no transaction, as count_no_trans. Time box: 10 minutes.

Send me in the chat: your query and the LeetCode result.

Optional quick answer: which column did you test with IS NULL, and why that one?

To pass:

Read next

Main source: JOIN Clause (MySQL 8.0 manual). Find the bullet "If there is no matching row for the right table…". Its example query is this lesson's recipe.

💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.