Missing Values: NULL
You know NULL from the first lesson: it means that a value is missing. The shop doesn't know Ava Green's city, and Robot Rebellion has no release date yet. To find such rows, it seems natural to write = NULL. But this query finds nothing:
SELECT name, city
FROM customers
WHERE city = NULL;| name | city |
|---|
That's not a mistake in the data. NULL means “unknown”, and comparing anything with an unknown value gives an unknown result. Is London equal to some unknown city? Nobody knows. Such a comparison is neither true nor false; its result is NULL again. And WHERE keeps only the rows for which the condition is true. Not even NULL = NULL is true: are two unknown cities the same? Unknown again. You can try it yourself: a SELECT without FROM reads no table, it just works out the values you write and shows them as one row:
SELECT 5 = NULL, NULL = NULL;| 5 = NULL | NULL = NULL |
|---|---|
| NULL | NULL |
That's why SQL has a special operator for missing values, IS NULL. The games that aren't out yet:
SELECT title, released
FROM games
WHERE released IS NULL
ORDER BY title;| title | released |
|---|---|
| Mystery Manor | NULL |
| Robot Rebellion | NULL |
Its opposite is IS NOT NULL, the rows that do have a value: WHERE released IS NOT NULL finds the 18 games that are out.
The same goes for every other comparison, <> included. Which customers aren't from London?
SELECT name, city
FROM customers
WHERE city <> 'London'
ORDER BY name;| name | city |
|---|---|
| Isla Young | Leeds |
| Jack Harris | Manchester |
| Liam Turner | Manchester |
| … | … |
Ava Green and Henry Scott aren't in the result. Is an unknown city different from London? Unknown again. If you want them too, you have to ask for them: city <> 'London' OR city IS NULL.
Typical mistake: = NULL instead of IS NULL. It isn't an error, so SQLite won't warn you; the query just finds nothing. Whenever you look for missing values, write IS NULL.
Instructions
The query in the editor should show the name and the email of the customers who have no city, but it finds nothing. Fix it:
| name | |
|---|---|
| Ava Green | ava.green@mail.com |
| Henry Scott | henry@scott.dev |
Below it, show the game ID, the stars and the comment of the five-star reviews that have a comment, sorted by game ID:
| game_id | stars | comment |
|---|---|---|
| 1 | 5 | Best RPG I have played in years! |
| 3 | 5 | So relaxing. |
| 6 | 5 | Perfect for the bus. |
| 7 | 5 | Amazing space battles. |
| 14 | 5 | Fast and fun. |
| 15 | 5 | Worth every penny. |
The shop is writing a newsletter about new games: those released in 2025 or later, and those that aren't out yet. Show their title and release date, sorted by release date and then by title:
| title | released |
|---|---|
| Mystery Manor | NULL |
| Robot Rebellion | NULL |
| Star Pilots | 2025-01-23 |
| Turbo Karts | 2025-06-05 |
| Goal Rush | 2025-09-26 |
| Knight's Oath | 2025-11-14 |
Start programming for free
11/12