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:
- Use the anti-join recipe:
LEFT JOIN, thenWHERE right.key IS NULL. - Combine it with
GROUP BYandCOUNT. - Explain why the test goes in
WHEREand not inON. - Solve LeetCode 1581 alone.
Three words you need
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;
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
IS NULL, never= NULL. You saw why in Lesson 3:= NULLis UNKNOWN, so it would drop every row.- Test the right table's key. Use a column that is never NULL in a real row, like
p.purchase_idorp.session_id. If you test a column likep.amountand a real purchase had a NULL amount, you'd wrongly count that session as empty. - The test goes in
WHERE, not inON.ONdecides how rows match.WHEREfilters the result (MySQL manual). If you writeLEFT JOIN … ON s.session_id = p.session_id AND p.purchase_id IS NULL, no purchase can ever match. LEFT JOIN still keeps every session, so you get all sessions back.
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:
- LeetCode says Accepted.
Visitsis the left table, and theIS NULLtest is inWHERE.- The output columns are
customer_idandcount_no_trans.
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.
- JOIN Clause 📄 Docs · 2 min (one bullet) · The official LEFT JOIN + IS NULL pattern, and the ON vs WHERE rule.
- Subqueries with EXISTS or NOT EXISTS 📄 Docs · 3 min · The "store present in no cities" example is the same question written another way.
- Working with NULL Values 📄 Docs · 3 min · A quick review of why
IS NULLis the only test that works.
💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.