Sometimes a value is missing: nobody knows Finn's city. A database stores that as NULL. NULL isn't zero and it isn't empty text. It means unknown.
That has a strange effect. Is an unknown city equal to Austin? Unknown. Is it equal to another unknown? Also unknown. A WHERE condition only keeps rows where the test is true, and "unknown" isn't true, so WHERE city = NULL matches no rows at all, not even Finn's. To test for a missing value, write IS NULL:
SELECT name
FROM customers
WHERE city IS NULL;
Returns
name
----
Finn
(1 row)
And IS NOT NULL for the rows that do have a value.
The same thing catches people out with "not equal". Customers who don't live in Austin:
SELECT name, city
FROM customers
WHERE city <> 'Austin';
Returns
name | city
-----+-------
Ben | Denver
Dev | Boston
Eve | Denver
Gus | Boston
(4 rows)
Finn isn't there. His city might be Austin, for all the database knows, so "not Austin" is unknown for him and his row is dropped. If you want him too, say so: WHERE city <> 'Austin' OR city IS NULL.
COALESCE(a, b) gives back a unless it's NULL, and b if it is. It's the usual way to show something friendlier than NULL:
SELECT name, COALESCE(city, 'Unknown') AS city
FROM customers
WHERE id >= 5;
Returns
name | city
-----+--------
Eve | Denver
Finn | Unknown
Gus | Boston
Hana | Austin
(4 rows)
(AS city names the result column. You'll use AS a lot in lesson 8.) When you sort, SQLite puts NULLs first; PostgreSQL and Oracle put them last. Most databases let you choose with NULLS FIRST or NULLS LAST.
Practice
The names of the customers whose city we don't know.