How do you handle NULL values in SQL?

Instruction: Provide an example query that demonstrates how to filter out records with NULL values in a 'birthdate' column.

Context: This question tests the candidate's ability to work with NULL values in SQL, including understanding of how NULL is treated in SQL and how to filter or handle these values.

Updated

Official answer

Use IS NOT NULL to keep records with a recorded birthdate:

SELECT user_id, birthdate
FROM users
WHERE birthdate IS NOT NULL
ORDER BY user_id;

NULL represents an absent or unknown value under the schema's meaning. Ordinary comparisons such as birthdate = NULL do not test for it; the result is unknown, and WHERE keeps only true conditions.

COALESCE supplies an alternative expression, but replacing a missing birthdate with a guessed date invents information. Use defaults only when their meaning is justified. Many aggregates ignore NULL arguments, while COUNT(*) counts rows; document the denominator and missingness.

NOT NULL enforces presence, not truth or completeness of the source record. Filtering out missing dates can also select a different population, so report how many records were excluded when that matters to the analysis.

Related Questions