Missing values: NULL
Stage 1: Basics · Lesson 5 of 17 · 5 min read · free, interactive
NULL means "unknown" or "missing". In players, a NULL country means we don't know where the player is from, and a NULL mentor_id means they have no mentor.
NULL is not a value you can compare with =. Any comparison with NULL is itself unknown, so this finds nothing:
SELECT name FROM players WHERE country = NULL;
Use IS NULL and IS NOT NULL instead:
SELECT name FROM players WHERE country IS NULL;
COALESCE(a, b) returns the first argument that isn't NULL — handy for showing a default:
SELECT name, COALESCE(country, '??') AS country FROM players;
Tip: <> has the same trap: country <> 'VN' skips players whose country is NULL.
Practice
Each answer runs in your browser and is checked on hidden data.
- Write the query: Return the
nameof every player who has no mentor. Row order does not matter. - Find the bug: Return the
nameandcountryof players whose country is known. Row order does not matter. - Fill in the blanks: Return every player's
nameandcountry, showing'Unknown'when the country is missing. Row order does not matter.
Previous: Removing duplicates with DISTINCT · Next: AND, OR and NOT · All lessons