All SQL topics
SQL TOPIC

SQL NULL: find and handle missing values

NULL means "unknown", so it never equals anything, not even another NULL. Test it with IS NULL.

WHERE city = NULL returns nothing; WHERE city IS NULL finds the gaps. Use IS NOT NULL for the opposite.

COALESCE(city, 'Unknown') replaces missing values in the output. Aggregates such as SUM and AVG skip NULLs, while COUNT(*) counts every row.

Worked example

Which customers have no city recorded?

SELECT name, country
FROM customers
WHERE city IS NULL;
Result on the sample data
namecountry
Lucas MartinFrance
Ciara WalshIreland

Common mistakes

  • Writing = NULL instead of IS NULL.
  • Forgetting that COUNT(column) ignores NULLs while COUNT(*) does not.