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 is not the same as zero, an empty string, or the word "null".
Because NULL means unknown, equality does not work the way it does for ordinary values. UseIS 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 does not 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 should not appear.
Schema · Garden ShopTable · orders6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
Fix the query so it returns unshipped orders: rows whereshipped date is missing. Exclude cancelled orders and sort from oldest to newest.
- Columns: order_id, order_date, shipped_date, status.
- Rows: 6 unshipped, non-cancelled orders.
- Every shipped_date value should display as NULL or blank.
You finished “Data Types Before Deep Querying.”
Nice work. Ready to start the next one?
Start Chapter 3: Select, Filter, and Sort →Begins with 3.1 Choosing columnsRelated
See why text, numbers, dates, and booleans behave differently.
Compare, sort, and calculate with numeric values.
Filter text values with string literals.
Compare and sort date values chronologically.