SQL Quest › SQL Interview Questions › NULL Handling
Default Age for Missing Records (COALESCE)
The passenger registry has missing age values for several rows. A demographics dashboard wants every row to display either the real age or 0 instead of NULL — so the chart bins don't crash on missing values.
Show passenger_id, name, age (the raw value, NULL allowed), and reported_age (COALESCE(age, 0)). Order by passenger_id. Limit to 15 rows.
COALESCE returns the first non-NULL argument from its list. It's the portable, ANSI-standard way to provide a default for NULL. SQLite also accepts IFNULL(age, 0) and MySQL has IFNULL, but COALESCE works everywhere.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
passengers
| passenger_id | survived | pclass | name | sex | age | sibsp | parch | fare | embarked |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | 7.25 | S |
| 2 | 1 | 1 | Cumings, Mrs. John Bradley | female | 38 | 1 | 0 | 71.28 | C |
| 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | 7.93 | S |
Expected output: reported_age = 0
Hint
SELECT, NULL Handling, COALESCE, and open the hint there if you stall.Concepts
SELECT NULL Handling COALESCE
Practise the topic: SQL practice questions · NULL handling practice
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test