MySQL NULL Values
NULL means "no value" or "unknown". It is not zero, not an empty string and not false. A customer whose e-mail is NULL simply hasn't given us one; a customer with e-mail '' gave us an empty string, which is a different thing.
Columns accept NULL unless they are declared NOT NULL. In the shop database, three customers have a NULL email, Ivan Petrov has a NULL city and orders that haven't shipped have a NULL shipped_date.
IS NULL and IS NOT NULL
You can't test for NULL with =. Any comparison with NULL gives NULL (unknown), and a WHERE filter only keeps rows where the condition is true:
SELECT COUNT(*) AS with_equals FROM customers WHERE email = NULL;
SELECT first_name, last_name FROM customers WHERE email IS NULL;+-------------+
| with_equals |
+-------------+
| 0 |
+-------------+
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Chloe | Martin |
| George | Brown |
| Mia | Jensen |
+------------+-----------+
IS NOT NULL is the opposite:
SELECT order_id, order_date, shipped_date
FROM orders
WHERE shipped_date IS NOT NULL AND order_date >= '2025-05-01';+----------+------------+--------------+
| order_id | order_date | shipped_date |
+----------+------------+--------------+
| 17 | 2025-05-03 | 2025-05-06 |
| 18 | 2025-05-12 | 2025-05-14 |
| 19 | 2025-05-20 | 2025-05-21 |
+----------+------------+--------------+
NULL in expressions
NULL is contagious: almost every operator or function returns NULL when one of its inputs is NULL.
SELECT 5 + NULL AS plus, CONCAT('a', NULL) AS concat_null,
NULL = NULL AS eq, NULL <=> NULL AS null_safe_eq;+------+-------------+------+--------------+
| plus | concat_null | eq | null_safe_eq |
+------+-------------+------+--------------+
| NULL | NULL | NULL | 1 |
+------+-------------+------+--------------+
The last column shows MySQL's null-safe equality operator <=>. It works like = but treats two NULLs as equal and returns 0 instead of NULL when only one side is NULL. Handy for comparing two nullable columns.
Counting and NULL
Aggregate functions skip NULLs. COUNT(*) counts rows, but COUNT(column) counts only non-NULL values in that column:
SELECT COUNT(*) AS customers,
COUNT(email) AS with_email,
COUNT(city) AS with_city
FROM customers;+-----------+------------+-----------+
| customers | with_email | with_city |
+-----------+------------+-----------+
| 15 | 12 | 14 |
+-----------+------------+-----------+
Replacing NULL in the output
IFNULL(value, fallback) and the standard COALESCE(a, b, ...) return the first non-NULL argument. They are covered in detail in the NULL Functions lesson:
SELECT first_name, IFNULL(email, '(no e-mail)') AS email
FROM customers
WHERE customer_id IN (2, 3, 7);+------------+------------------------+
| first_name | email |
+------------+------------------------+
| Ben | ben.carter@example.com |
| Chloe | (no e-mail) |
| George | (no e-mail) |
+------------+------------------------+
Tip: Decide deliberately whether a column may be NULL. If "unknown" is a meaningful state (a shipped date that doesn't exist yet), allow NULL. If every row must have a value, declare the column
NOT NULLand let the database enforce it.
Test yourself
Which condition finds customers without an e-mail address?
- email = NULL
- email IS NULL
- email == NULL
- email = ''
What is SELECT 10 + NULL;?
- 10
- 0
- NULL
- An error
Three of 15 customers have a NULL email. What does SELECT COUNT(email) FROM customers return?
- 15
- 12
- 3
- NULL