The NULL trap: how blank cells move totals and means
A blank cell is not zero. NULL means no value, and no comparison with it is ever true. Miss that rule and a count or an average drifts without warning.
NULL never compares equal
NULL = NULL is NULL, not true, so test with IS NULL and IS NOT NULL. WHERE col <> 'x' drops rows where col is NULL — an easy place to assume those rows were included because nothing matched them.
A NULL inside a NOT IN list makes the whole condition unsatisfiable. Leave NULLs out of the list or rewrite as NOT EXISTS.
| Expression | Result | Note |
|---|---|---|
| NULL = NULL | NULL | neither true nor false |
| NULL <> 1 | NULL | filtered out by WHERE |
| 1 + NULL | NULL | the whole arithmetic result |
| COALESCE(NULL, 0) | 0 | first non-NULL |
| NULLIF(0, 0) | NULL | NULL when equal |
The denominator of COUNT and AVG
COUNT(*) counts rows, COUNT(col) counts non-NULL values. With 3% missing revenue the two differ by 3%. AVG(col) also divides by the number of present values, so it differs from an average over zero-filled data.
Decide up front whether a reported average treats missing revenue as zero or excludes it, and write the choice down. Otherwise two people report different averages from the same table.
SELECT COUNT(*) AS rows,
COUNT(revenue) AS with_revenue,
ROUND(AVG(revenue), 0) AS avg_present,
ROUND(AVG(COALESCE(revenue, 0)), 0) AS avg_zero_filled
FROM orders;
Filling with zero versus leaving blank
COALESCE(col, 0) is safe for sums: adding zero does not change a total. In a mean it lowers the result, so show the present-values mean and the zero-filled mean separately.
NULLIF works the other way. NULLIF(x, 0) turns zero into NULL, avoiding division by zero when computing a ratio.
SELECT region_id,
ROUND(SUM(revenue) / NULLIF(COUNT(*), 0), 0) AS revenue_per_order
FROM orders
GROUP BY region_id
ORDER BY revenue_per_order DESC;
Count missing values first
On any new table, count nulls per column before anything else. Under 5% is usually tolerable; above 30%, an average over that column is misleading. The profile panel does this automatically, so look at it before writing the query.
Separating NULL from an empty string
In CSV data an empty field becomes NULL, but a quoted empty value can arrive as an empty string. Count both with WHERE col IS NULL OR col = '', or test LENGTH(col) = 0 when the distinction matters.
Report missing values with the number
Any metric built on a column with gaps should carry its value count. Write "average revenue 1,240,000 (232 values, 8 missing)" so the reader knows the denominator, and state it when missing values were filled with zero.
A column that is 30% empty may not belong in the metric at all: its average describes the other 70%, and the smaller the sample the larger that distortion.
Questions
When should NULL not become zero?
In averages, ratios and ranks. Zero is indistinguishable from a real measurement and distorts the distribution. Sums and counts are safe.
Is an empty string the same as NULL?
No. An empty CSV field becomes NULL, but a quoted "" can arrive as an empty string. Check with LENGTH(col) = 0.
Sources
Every query in this guide runs against the built-in sample data.
Open the lab