Fix the bug: rows go missing, no error
Every lesson so far either handed you working code or asked you to write it
from scratch. Real work is often a third thing: someone else's query already
runs, returns a result, and nothing looks wrong at a glance - it's just
missing rows. No traceback, no red error text, nothing pointing at a line
number. The only way to catch it is to actually read what came back and
notice it's short.
The query below runs without error. It just drops anyone who hasn't placed an
order - a plain JOIN only keeps rows where both sides match, so a user
with zero orders has nothing on the orders side to match against, and
vanishes from the result instead of showing up with an empty order. That's
the classic gap between JOIN (inner join) and LEFT JOIN: inner join keeps
matches only, left join keeps every row from the left table and fills in
NULL where there's no match on the right.
Same users and orders tables as the earlier JOIN lessons - six users,
five orders, and two users (Ben and Sam) who haven't ordered anything.
Your task: return every user's name alongside the item they ordered -
including users who haven't ordered anything, with NULL for their item.
You'll practice:
Recognizing when JOIN silently drops rows instead of erroring
Switching to LEFT JOIN to keep every row from one side regardless of a match
Show a hint
Show solution
Previous Next