Checking for NULL values #
NULL represents a missing or unknown value. It isn't equal to anything, not even to another NULL, so = NULL never matches. To check for it, SQL gives us the dedicated IS NULL (and IS NOT NULL) operators.
Syntax #
SELECT
column_name
FROM
table_name
WHERE
column_name IS NULL;
SELECT
column_name
FROM
table_name
WHERE
column_name IS NOT NULL;
Example #
In the orders table, shipped_date is NULL whenever an order hasn't shipped yet, that is, when its status is 'Pending' or 'Cancelled'. We can find those orders directly:
SELECT
*
FROM
orders
WHERE
shipped_date IS NULL;
Result:
+----+-------------+------------+-----------+--------------+
| id | customer_id | order_date | status | shipped_date |
+----+-------------+------------+-----------+--------------+
| 1 | 3 | 2025-... | Cancelled | None |
| 4 | 1 | 2025-... | Cancelled | None |
| 17 | 9 | 2025-... | Pending | None |
| .. | ... | ... | ... | ... |
If we wanted the opposite, the orders that have shipped, we would use WHERE shipped_date IS NOT NULL instead.
Exercise #
Write a query that retrieves all columns from the orders table, keeping only the rows where shipped_date is NULL.
- The result has a
shipped_datecolumn. - The result contains exactly the rows where
shipped_dateisNULL, and no others.
Solution
SELECT
*
FROM
orders
WHERE
shipped_date IS NULL;
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity