SQL NULL Handling

NULL means "unknown", not zero and not empty. Most wrong answers in SQL rounds come from a NULL or a division nobody checked.

SQL NULL handling cheat sheet: a three-valued logic table, the NOT IN trap, aggregates that skip NULLs, and NULL sort order by engine
Printed sheet title: NULL pitfalls and common mistakes

What the sheet covers

WHERE and HAVING keep a row only when the condition is TRUE; NULL is dropped. In ON, FALSE or NULL means no match; a LEFT JOIN still keeps the left row. x = NULL is never true: use IS NULL. NOT IN with one NULL returns no rows; use NOT EXISTS. Aggregates skip NULLs, so AVG divides by the non-NULL count (on SQL Server, AVG of integers is an integer), and SUM of no rows is NULL unless you write COALESCE(SUM(x), 0). COUNT(x) returns 0 on only-NULL values; COUNT(*) returns 0 only on zero rows. NULLIF turns a zero denominator into NULL. Dividing by zero errors in PostgreSQL and SQL Server, returns inf in DuckDB, and returns NULL in MySQL. In PostgreSQL and SQL Server, 1 / 2 is 0 for integers: multiply by 1.0 first. NULL sort order differs by engine; say NULLS LAST (or the CASE equivalent) explicitly. DISTINCT after a join hides fan-out in the rows but not in the sums.

Topics on the sheet

  • Three-valued logic
  • NOT IN with a NULL
  • Aggregates skip NULLs
  • COALESCE vs NULLIF, and integer division
  • NULL sort order
  • DISTINCT as a bandage

Download the PDF

NULL pitfalls and common mistakes as a one-page PDF in Letter and A4, plus all six sheets in one SQL cheat sheet PDF. Free PDF, download it right after you sign up. Signing up joins the dataengineers.club newsletter and unlocks the PDF bundle in Letter and A4. Unsubscribe anytime.

Three-valued logic

WHERE and HAVING keep a row only when the condition is TRUE. NULL is dropped, like FALSE. In ON, FALSE or NULL means no match; a LEFT JOIN still keeps the left row. So x = NULL is never true: use IS NULL. And status <> 'paid' drops NULL statuses too: use IS DISTINCT FROM 'paid' (MySQL: <=>; SQL Server 2022+).

expressionresult
NULL = NULLNULL
NULL <> 1NULL
NULL AND FALSEFALSE
NULL OR TRUETRUE
NOT (NULL)NULL

NOT IN with a NULL

x NOT IN (2, NULL) means x <> 2 AND x <> NULL, and that is never TRUE.

-- o.cid has a NULL: 0 rows
SELECT * FROM c
WHERE cid NOT IN (
  SELECT cid FROM o);

-- fix: NOT EXISTS (rows 1 and 3)
SELECT * FROM c
WHERE NOT EXISTS (
  SELECT 1 FROM o
  WHERE o.cid = c.cid);

Aggregates skip NULLs

On x = 5, NULL, 5, 7: COUNT(*) is 4, COUNT(x) is 3, COUNT(DISTINCT x) is 2, SUM(x) is 17, AVG(x) is 5.67, AVG(COALESCE(x, 0)) is 4.25. AVG divides by 3, not 4. Decide which one the question wants. On SQL Server, AVG of integers is an integer (5 and 4 here).

  • On zero rows, or only NULLs, SUM is NULL, not 0: write COALESCE(SUM(x), 0). COUNT(x) returns 0. COUNT(*) returns 0 only on zero rows.
  • MIN and MAX ignore NULLs too. GROUP BY and DISTINCT put all NULLs in one group.

COALESCE vs NULLIF, and integer division

COALESCE(phone, email, 'none')
amount / NULLIF(qty, 0)  -- NULL
-- a rate that works everywhere
1.0 * buyers / NULLIF(visitors, 0)
  • COALESCE(a, b): first value that is not NULL: COALESCE(discount, 0).
  • NULLIF(a, b): NULL when a = b, else a. amount / NULLIF(qty, 0) returns NULL instead of dividing by zero.
  • PostgreSQL and SQL Server: 1 / 2 is 0 for integers. DuckDB: / returns a double, // is integer division. Snowflake, BigQuery: a decimal or float. MySQL: a decimal (DIV is integer division).
  • Multiply before dividing: in PostgreSQL and SQL Server 1.0 * (1 / 2) is still 0.

Dividing by zero raises an error in PostgreSQL and SQL Server. DuckDB / returns inf. MySQL returns NULL.

NULL sort order

Say it explicitly: ORDER BY ts DESC NULLS LAST (DuckDB, PostgreSQL, Snowflake, BigQuery).

defaultASCDESC
PostgreSQL, Snowflakelastfirst
SQL Server, MySQL, BigQueryfirstlast
DuckDBlastlast
-- SQL Server and MySQL:
ORDER BY CASE WHEN ts IS NULL
         THEN 1 ELSE 0 END,
         ts DESC

It matters for "latest row": with ROW_NUMBER() OVER (ORDER BY ts DESC), PostgreSQL and Snowflake rank a NULL timestamp first.

DISTINCT as a bandage

Duplicates after a join mean a fan-out (SQL joins). DISTINCT hides it in the rows but not in the sums, and it also deletes rows that were real.

-- hides the fan-out
SELECT DISTINCT c.cid, c.name
FROM c JOIN o ON o.cid = c.cid;
-- says what you mean
SELECT c.cid, c.name FROM c
WHERE EXISTS (SELECT 1 FROM o
  WHERE o.cid = c.cid);

SUM(DISTINCT amount) counts each amount once, so two orders of 50 become 50.

Engines named on the sheet

DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server, MySQL. NULL sort order, integer division and NULLS LAST notes name DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server and MySQL. DuckDB 2.0.1 is the runtime used in the book. Do not read this list as a claim that every engine was executed for every example.

FAQ

Why does x = NULL never return rows?

WHERE and HAVING keep a row only when the condition is TRUE, and x = NULL is NULL, not TRUE. Use IS NULL. In ON, FALSE or NULL means no match, but a LEFT JOIN still keeps the left row.

Why does NOT IN return no rows when the list has a NULL?

x NOT IN (2, NULL) means x <> 2 AND x <> NULL, which is never TRUE. Use NOT EXISTS instead.

Do aggregates count NULLs?

No. COUNT(x), SUM, AVG, MIN and MAX skip NULLs, so AVG divides by the non-NULL count (on SQL Server, AVG of integers is an integer). On zero rows or only NULLs, SUM is NULL: write COALESCE(SUM(x), 0). COUNT(x) returns 0, and COUNT(*) returns 0 only on zero rows.

Why is 1 / 2 equal to 0?

In PostgreSQL and SQL Server, dividing two integers returns an integer. Multiply by 1.0 before dividing: 1.0 * buyers / NULLIF(visitors, 0). Dividing by zero errors in PostgreSQL and SQL Server, returns inf in DuckDB, and returns NULL in MySQL.

© 2026 Vincent Charles. License: CC BY-NC-ND 4.0. You may share this page with credit. Do not sell the sheets or share modified versions.

SQL cheat sheets by Vincent Charles, from SQL Interview Questions for Data Engineers. dataengineers.club