SQL · Chapter 13 of 42
SQL NULL Values
NULL represents 'unknown' or 'missing' — NOT zero, NOT empty string.
Comparisons with NULL always yield UNKNOWN, so `WHERE col = NULL` returns nothing.
IS NULL / IS NOT NULL
The only correct way to test for NULL.
COALESCE / IFNULL
`COALESCE(a, b, c)` returns the first non-NULL value in the list.
Example 1 (sql)
SELECT * FROM users WHERE phone IS NULL;Output
users without a phoneCorrect NULL check.
Example 2 (sql)
SELECT name, COALESCE(nickname, name) AS display_name FROM users;Output
uses nickname if set, else nameFallback pattern with COALESCE.
Key points
- NULL ≠ 0 ≠ ''.
- `= NULL` NEVER matches — use `IS NULL`.
- COALESCE picks the first non-NULL argument.
- Aggregates like SUM() ignore NULLs.
💡 Note: COUNT(*) counts all rows; COUNT(col) counts only rows where col IS NOT NULL.
