Missing Values with SQL NULL
NULL marks missing or unknown data, so it needs special comparison syntax.
NULL
means a value is missing or unknown. It isn't the same as zero, an empty
string, or the word "null".
Because
NULL
marks a missing or unknown value, equality doesn't work the way it does for
ordinary values: ordinary comparisons involving NULL evaluate to
UNKNOWN. Use
IS NULL
to find missing values and
IS NOT NULL
to keep rows where a value is present.
select id_column, nullable_column
from table_name
where nullable_column is null
and status_column <> 'cancelled' The common mistake
= NULL
doesn't find missing values. It evaluates to unknown, so the
WHERE
clause drops the row. Reach for
IS NULL
or
IS NOT NULL
whenever the question is about whether a value exists.
Check the result by looking at the nullable column itself. In this lesson,
every returned
shipped_date
should be blank or NULL, and cancelled orders shouldn't appear.
NULL changes counting too:
COUNT(column)
skips missing values, while
COUNT(*)
counts every row. See
why COUNT(column) ignores NULL.
Schema · Garden Shop Table · orders 6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
Fix the query so it returns unshipped orders: rows where shipped date is missing. Exclude cancelled orders and sort from oldest to newest.
Run your query to inspect the result.
Expected answer
- Columns: order_id, order_date, shipped_date, status.
- Rows: 6 unshipped, non-cancelled orders.
- Every shipped_date value should display as NULL or blank.
Related
Date parts, math, and truncation.
IS NULL, COALESCE, NULLIF, and null-safe equality.
Use IS NULL instead of equality.
Length, case, trimming, slicing, and concatenation.
A stray NULL drops every row. Use NOT EXISTS.
IS NULL, COALESCE, NULLIF, and null-safe logic.