SQL QuestSQL Interview Questions › NULL Handling

Default Age for Missing Records (COALESCE)

EasyFreeQuerying BasicsNULL Handling

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_idsurvivedpclassnamesexagesibspparchfareembarked
103Braund, Mr. Owen Harrismale22107.25S
211Cumings, Mrs. John Bradleyfemale381071.28C
313Heikkinen, Miss. Lainafemale26007.93S

Expected output: reported_age = 0

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for 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

Handling NULL ValuesEasy · FreeLEFT JOIN: Watch the NULLs AppearEasy · FreeFailures with No Loss EstimateEasy · FreeProperties Without Listed OwnerEasy · FreeChargeback Reason Codes: Resolved and Still OpenEasy · FreeGenre Box Office ReportMedium · Free

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