SQL NULL Values
NULL means "no value" / "unknown". It is not zero, not an empty string and not 'NULL' — it is the absence of a value. In TAD Market, four customers have no e-mail address, and orders that have not shipped yet have no shipped_date.
Testing for NULL
You cannot find NULLs with =. Any comparison with NULL — even NULL = NULL — is unknown, and WHERE drops unknown rows. Use IS NULL and IS NOT NULL instead:
SELECT customer_id, name, email
FROM customers
WHERE email IS NULL
ORDER BY customer_id;customer_id | name | email
------------+-----------------+------
5 | Le Petit Marche | NULL
12 | Omar Murphy | NULL
18 | Felix Reyes | NULL
23 | Zara Ali | NULL
(4 rows)
-- The wrong way: returns nothing
SELECT COUNT(*) AS wrong FROM customers WHERE email = NULL;
-- The right way
SELECT COUNT(*) AS has_email FROM customers WHERE email IS NOT NULL;wrong
-----
0
(1 row)
has_email
---------
21
(1 row)
NULL in expressions
Most operations involving NULL produce NULL:
SELECT 5 + NULL AS plus,
'abc' || NULL AS concat,
NULL = NULL AS equal,
NULL IS NULL AS is_null,
NULL > 3 AS greater;plus | concat | equal | is_null | greater
-----+--------+-------+---------+--------
NULL | NULL | NULL | 1 | NULL
(1 row)
(SQLite shows true as 1 and false as 0.)
Unshipped orders
SELECT status, COUNT(*) AS orders
FROM orders
WHERE shipped_date IS NULL
GROUP BY status
ORDER BY status;status | orders
-----------+-------
cancelled | 7
processing | 8
(2 rows)
Every processing and cancelled order has a NULL shipped_date — exactly what we would expect.
NULLs and aggregate functions
Aggregates such as COUNT(column), SUM and AVG skip NULLs. COUNT(*) counts rows, whatever their content:
SELECT COUNT(*) AS all_customers,
COUNT(email) AS with_email,
COUNT(*) - COUNT(email) AS without_email
FROM customers;all_customers | with_email | without_email
--------------+------------+--------------
25 | 21 | 4
(1 row)
Replacing NULL for display
COALESCE(a, b, …) returns its first non-NULL argument — great for showing a placeholder:
SELECT name, COALESCE(email, '(no email)') AS email
FROM customers
WHERE country IN ('Italy', 'India', 'Mexico', 'Turkey', 'Ireland')
ORDER BY name;name | email
----------------+-------------------------
Felix Reyes | (no email)
Isla Brennan | isla.brennan@example.com
Le Petit Marche | (no email)
Omar Murphy | (no email)
Zara Ali | (no email)
(5 rows)
The NULL Functions lesson covers COALESCE, IFNULL and NULLIF in detail.
Summary
| Do | Don't |
|---|---|
email IS NULL |
email = NULL |
email IS NOT NULL |
email <> NULL |
COALESCE(email, '-') to display a default |
assume NULL behaves like '' or 0 |
Note: SQLite also has
ISandIS NOTfor any value:a IS bis true when both are NULL or both are equal. The standard spelling isa IS NOT DISTINCT FROM b.
Test yourself
Which condition finds orders without a shipped date?
- shipped_date = NULL
- shipped_date = ''
- shipped_date IS NULL
- shipped_date == 0
What is COUNT(email) when 4 of 25 customers have a NULL email?
- 25
- 21
- 4
- NULL
What does 10 + NULL evaluate to?
- 10
- 0
- NULL
- An error