Skip to main content
SQLIterixs

SQL NULL: why rows disappear from your query

An unknown value is neither zero nor an empty string. Learn how NULL affects filters, counts and averages before filling in missing data.

You have three orders: one has a discount, one has none, and the third has no recorded discount yet. Filtering with discount = 0 returns only one order. That is correct: an unknown discount is not necessarily zero.

SQL uses NULL for an unknown or absent value. It is neither zero nor the empty string ''. Working with it introduces a third outcome for comparisons: unknown, alongside true and false.

Three rows are enough

Run this whole block in SQLite, PostgreSQL or the online SQL editor, with nothing to install. WITH gives a temporary name to three rows, so you need no stored tables:

sql
WITH orders(id, discount) AS (
  VALUES (1, 0), (2, 10), (3, NULL)
)
SELECT id, discount
FROM orders
WHERE discount = 0;
id | discount
---|---------
1 | 0

Order 2 has a discount of ten. We do not know the discount for order 3. Comparing NULL = 0 yields unknown. WHERE keeps only rows whose condition is true.

WHERE keeps only TRUE
WHERE keeps only TRUESELECT id, discountFROM ordersWHERE discount = 0;iddiscountdiscount = 010TRUE210FALSE3nullUNKNOWNkeep rowdrop rowdrop rowNULL = 0 is not FALSE: it is UNKNOWN,and WHERE drops that row too

Changing the filter to discount <> 0 returns order 2, but still excludes order 3. Negating an unknown comparison does not turn it into a known result.

Find missing values with IS NULL

Do not write discount = NULL to find absent values. Use IS NULL, or IS NOT NULL for the opposite:

sql
WITH orders(id, discount) AS (
  VALUES (1, 0), (2, 10), (3, NULL)
)
SELECT id
FROM orders
WHERE discount IS NULL;
id
--
3

For “orders with zero discount or a discount not yet recorded,” write WHERE discount = 0 OR discount IS NULL. You are deliberately combining two different groups.

COUNT has two meanings here

COUNT(*) counts rows. COUNT(discount) counts rows where that column is not NULL:

sql
WITH orders(id, discount) AS (
  VALUES (1, 0), (2, 10), (3, NULL)
)
SELECT COUNT(*) AS orders,
       COUNT(discount) AS known_discounts,
       AVG(discount) AS average_known_discount
FROM orders;
orders | known_discounts | average_known_discount
-------|-----------------|-----------------------
3 | 2 | 5.0

PostgreSQL displays it as 5.0000000000000000; the value is the same. The average is five because it uses 0 and 10. It does not divide by three. Before calling this the average discount across all orders, mention the missing observation. It changes what a reader can conclude from the report.

COALESCE supplies a fallback, not a fact

COALESCE returns its first non-NULL argument. It can provide a display fallback or implement an explicit business rule:

sql
WITH orders(id, discount) AS (
  VALUES (1, 0), (2, 10), (3, NULL)
)
SELECT id, COALESCE(discount, 0) AS display_discount
FROM orders
ORDER BY id;
id | display_discount
---|-----------------
1 | 0
2 | 10
3 | 0

Order 3 now displays zero. The query neither changes the source data nor proves that zero is correct. Put the replacement inside AVG and you change the calculation: that order now contributes a zero.

An honest report might display “pending” in the interface while preserving the missing number. Avoid mixing strings and numbers in COALESCE(discount, 'pending') and expecting every database to accept the types.

Check your understanding

Predict the IDs returned by discount IS NOT NULL, discount <> 10, and discount = 0 OR discount IS NULL. The answers are 1 and 2, 1, and 1 and 3. If you expected order 3 in the second answer, revisit the WHERE rule.

This distinction comes up again with LEFT JOIN, which can produce NULL values when there is no match. Continue with counting orders without losing customers review the glossary definition of NULL and read the PostgreSQL comparison documentation.

Share

LinkedIn