When you need it
Every real table has missing values. The question may say it outright ("customers with no email", "treat missing as zero") or hide it ("average rating", "how many orders had a coupon"). Words that signal it: "missing", "unknown", "not given", "blank", "if any", "never".
NULL means "unknown", not zero and not an empty string. Any comparison with an unknown gives unknown, and WHERE keeps only rows where the test is true.
The shape of the query
select coalesce(col, 'unknown') as label,
count(*) as rows_total,
count(col) as rows_with_value
from t
where col is null or col <> 'x'
group by 1;Step by step:
- Ask which columns can be NULL. The world notes say so.
- Test with
IS NULLandIS NOT NULL, never= NULL. - Replace NULL with a label or default using
COALESCEwhere the prompt asks for it. - Pick the right count:
COUNT(*)for rows,COUNT(col)for rows that have a value.
select coalesce(referral_source, 'unknown') as source,
count(*) as customers,
count(email) as with_email
from customers
group by coalesce(referral_source, 'unknown')
order by customers desc, source;Why <> loses rows:
select count(*) filter (where referral_source <> 'organic') as naive,
count(*) filter (where referral_source is distinct from 'organic') as null_safe,
count(*) filter (where referral_source is null) as unknown_source
from customers;The null_safe column is the number most people mean by "not organic". The naive <> skips every customer whose source is unknown, so null_safe is larger by exactly unknown_source.
Traps interviewers look for
<>andNOT INskip NULLs.col <> 'x'is unknown whencolis NULL, so the row disappears. Addor col is null, or useIS DISTINCT FROM.= NULL. It is never true. Always useIS NULL.COUNT(col)versusCOUNT(*). The first ignores NULLs. A "percent with a coupon" built on the wrong one is off.AVGignores NULLs. The average of 4, 5 and NULL is 4.5, not 3. If a missing rating should count as zero, sayavg(coalesce(rating, 0)). If not, the default is probably what you want. State which.- NULL arithmetic.
price + NULLis NULL. One missing shipping fee wipes out a whole sum of row totals unless youCOALESCEit. - Sorting. PostgreSQL puts NULLs last in ascending order and first in descending order, so a "top 5" can fill with unknowns. Use
nulls lastor filter them out. - Joins on NULL keys. NULL never equals NULL, so those rows vanish from an inner join. A left join keeps them with NULLs on the right.
- Empty string is not NULL.
''and NULL are different. Clean both if you are counting "blank" values.
Variations
NULLIF(a, b). Returns NULL whena = b. The standard way to avoid divide by zero, and to turn''into NULL.COALESCEchains.coalesce(mobile, home, work)picks the first value that exists.IS DISTINCT FROM. A NULL-safe<>.IS NOT DISTINCT FROMis a NULL-safe=.- Anti-join on missing data. "Customers who never ordered" is a missing row, not a NULL column. See the next lessons.
- Three-valued logic.
NULL AND falseis false andNULL OR trueis true, butNULL AND trueis NULL.
In other warehouses
- MySQL has the NULL-safe equality operator
<=>. Snowflake, BigQuery, PostgreSQL and SQL Server 2022 supportIS NOT DISTINCT FROM. - SQL Server and Snowflake have
ISNULL(a, b)andIFNULL(a, b)as two-argument shortcuts.COALESCEworks everywhere, so prefer it. - BigQuery has
IFNULLandSAFE_DIVIDE. Snowflake hasZEROIFNULLandNVL. - Sort order of NULLs differs by engine. See the ranking lesson.