$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #253

Which fix removes the bug?

SELECT AVG(score) FROM results;
-- scores: 10, NULL, 20
-- expected 10 (a missing score counts as 0)
  1. Use AVG(COALESCE(score, 0))
  2. Use AVG(DISTINCT score)
  3. Use SUM(score) / COUNT(score)
  4. Add WHERE score IS NOT NULL
AVG skips NULLs, giving 15. COALESCE turns the NULL into 0 so it is counted: (10 + 0 + 20) / 3 = 10.
Play Problem Solving ▸