5 MIN · 2026-10-31

NULL in SQL: the value that isn't there

NULL is unknown, not zero. Why `= NULL` returns nothing, how COUNT(column) skips NULLs, and when COALESCE and NOT IN surprise you.

NULL in SQL: the value that isn't there

NULL is not zero, not an empty string, and not false. It means unknown — and that one idea explains most of the surprises.

You cannot compare against NULL

This looks reasonable and returns nothing:

SELECT name FROM artists WHERE formed_year = NULL;

NULL = NULL is unknown, not true, so the WHERE clause cannot accept the row. The correct test is IS NULL:

SELECT name, formed_year FROM artists WHERE formed_year IS NULL;

Two artists come back: Nocturne Quartet and The Paper Cranes. Their formation year was never recorded.

COUNT(*) and COUNT(column) are different

COUNT(*) counts rows. COUNT(column) counts non-NULL values:

SELECT COUNT(*) AS artists, COUNT(formed_year) AS with_year
FROM artists;

Twelve artists, ten known years. The gap is the two NULLs — a quick way to measure missing data.

COALESCE fills the gap

COALESCE returns the first argument that is not NULL:

SELECT name, COALESCE(formed_year, 'Unknown') AS formed_year
FROM artists
WHERE formed_year IS NULL;

The column now mixes numbers and text, because that is what the query asked for. In a report, that is usually the point: a readable label instead of a blank cell.

NOT IN has a NULL trap

This query looks like “everyone formed outside 1998 and 2010”:

SELECT name FROM artists WHERE formed_year NOT IN (1998, 2010);

It returns the artists with a known year outside that list — and silently drops the two NULL rows. NOT IN compares each value with every item in the list; when one comparison is unknown, the row does not pass. If NULLs must survive, say so explicitly:

SELECT name FROM artists
WHERE formed_year IS NULL OR formed_year NOT IN (1998, 2010);

Filtering safely

Put the NULL check first, then the range:

SELECT name FROM artists
WHERE formed_year IS NOT NULL AND formed_year < 1990;

Three rows: Blue Meridian (1975), The Delta Kings (1962) and Celia Mar (1988). The IS NOT NULL is redundant here because < already excludes NULLs, but it documents the intent — and it matters as soon as you switch to NOT IN.

Keep exploring: the filtering chapter covers IS NULL, comparisons and three-valued logic with seven graded exercises.

Keep learning