Before you start, you need
Ask whether a value is missing or present
A customer may have no email address on record. In that case the table stores NULL: a missing value. It is not the number 0, an empty string or the word 'NULL' stored as text.
To find missing emails, use email IS NULL. To find emails that are present, use email IS NOT NULL. These conditions ask directly whether there is a value, without comparing it with a particular address.
email = NULL does not mean “no email”. An ordinary comparison with a missing value gives an unknown result. Because WHERE only keeps conditions that are true, that equality does not find the rows you expected.
An empty email may also need reviewing, but it is still text that is present. Keep that distinction: do not turn a missing value into 0 or into text just to make it easier to search for.
WHERE email IS NULL
-- To check presence: email IS NOT NULLSee it in action
Which customers have no email address saved?
| name | |
|---|---|
| Amy Wilson | amy@example.test |
| David Clark | NULL |
Before running: Does a missing email address satisfy email = NULL? What should that comparison return?
SELECT name, email
FROM customers
WHERE email IS NULL;| name | |
|---|---|
| David Clark | NULL |
| Peter Hill | NULL |
| Miles Lee | NULL |
- IS NULL keeps the three rows whose email is missing. name is still present, so you can tell which customer it is.
- An empty address or the text 'NULL' in quotes would be values that are present, so they would not meet this condition.
- email = NULL does not find those rows: the comparison is unknown, and WHERE only keeps conditions that are true.
- For the opposite question, customers with an email saved, use IS NOT NULL.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Watch a comparison come out unknown
Run a query for name and email using email = NULL. The output should be empty. This activity looks at a deliberate conceptual mistake.
Expected result: 0 rows with the columns name, email, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Before running it, predict: how many rows will email = NULL return? This activity expects an empty output on purpose.
Write SELECT name, email FROM customers with the condition WHERE email = NULL and compare the result with your prediction.
The query raises no error, but it finds no rows either: a comparison with NULL is never true. To find missing values you use IS NULL (next activity).
Solution explained
SELECT name, email FROM customers WHERE email = NULL;The output is empty because no comparison with NULL is ever true. It does not prove that every customer has an email.
Find the emails that are present
Guided practice
Find the emails that are present
Write a query that returns the name and email of the customers who do have an email.
Expected result: 9 rows with the columns name, email, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Keep the rows whose email is present.
Do not use <> NULL.
Select name and email.
Solution explained
SELECT name, email FROM customers WHERE email IS NOT NULL;IS NOT NULL keeps the rows that contain a known value.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Combine absence with another condition
Show name and city of the active customers (active = 1) who have no email.
Expected result: 2 rows with columns name, city, in that order. The order of the rows does not matter.
View tables and data
Open this panel to see the full data.
There are two requirements that must be met at the same time: being active and having no email.
Absence is checked with IS NULL, not with = NULL.
The condition is active = 1 AND email IS NULL.
Solution explained
SELECT name, city FROM customers WHERE active = 1 AND email IS NULL;David Clark and Miles Lee meet both conditions. Peter Hill has no email either, but is not active. You can filter by email without displaying it.
Key takeaways
- NULL represents a missing value.
- IS NULL checks for a missing value; IS NOT NULL checks for a present one.
- An ordinary comparison with NULL is not true.