What NULL Really Means
Unknown, not zero and not empty.
Meet NULL
NULL is SQL's way of saying "I don't know". It is not zero, not an empty string, and not false — it represents the absence of a value.
Whenever a column has no known value, the database stores NULL there. Learning how NULL behaves is one of the most important steps to writing correct queries.
-- A customers table where some phone numbers are unknown
SELECT id, name, phone
FROM customers;
-- id | name | phone
-- ---+---------+------------
-- 1 | Alice | 555-0101
-- 2 | Bob | NULL <- phone unknown
-- 3 | Carol | NULLNULL Is Not Zero
A common mistake is treating NULL like the number 0. They are completely different.
0is a known value: zero.NULLmeans the value is missing or unknown.
If an account balance is 0, the customer has no money. If it is NULL, we simply don't know the balance yet.
SELECT id, balance
FROM accounts;
-- id | balance
-- ---+--------
-- 1 | 0 <- known: empty wallet
-- 2 | NULL <- unknown: not recorded yetAll lessons in this course
- What NULL Really Means
- IS NULL and IS NOT NULL
- COALESCE and NULLIF
- NULLs in Aggregates and Joins