Bug hunt: fix code you did not write

PremiumAdvanced25 min3 exercises

A broken SQL query rarely errors. It runs, returns a neat table, and the table is wrong. Nobody notices until the report goes out with a customer missing. This lesson is three queries like that, written by a colleague in a hurry.

Check a query's answer the way you would check a sum:

The three classics here: JOIN silently drops rows with no match, WHERE filters rows before grouping while HAVING filters groups after, and any comparison with NULL is never true.

The rest of this lesson is Premium

Three SQL queries that run fine and give the wrong answer: an inner join that drops people, WHERE doing HAVING's job, and the NULL trap.

Module
SQL Advanced: Change data safely, nest queries inside queries, then fix three queries that run fine and answer the wrong question.
Challenges
  • Where did Dev go?
  • Big spenders
  • The missing orders

First challenge: Where did Dev go?

The manager wants every customer with their number of orders, including customers with none. Dev has never ordered, so he should be there with 0. Fix the query.

£7.99 a month, or £59 a year

Premium opens every Core and Advanced lesson in all four tracks, with the editor, checker and hints, plus the bigger projects. Cancel any time.

See Premium

Or keep going for free: SQL Foundations and the playground are free for good.