Module 2 · Filtering data

Work correctly with NULL values

By the end you will be able to search for absent or present values with IS NULL.

About 12 min

0 of 52 lessons

Before you start, you need

Filter rows with WHERE · Compare values in your filters

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.

Minimal syntax
WHERE email IS NULL
-- To check presence: email IS NOT NULL

See it in action

Which customers have no email address saved?

Sample data: 2 rows
nameemail
Amy Wilsonamy@example.test
David ClarkNULL

Before running: Does a missing email address satisfy email = NULL? What should that comparison return?

SQL
SELECT name, email
FROM customers
WHERE email IS NULL;
Example result: 3 rows
nameemail
David ClarkNULL
Peter HillNULL
Miles LeeNULL
  1. IS NULL keeps the three rows whose email is missing. name is still present, so you can tell which customer it is.
  2. An empty address or the text 'NULL' in quotes would be values that are present, so they would not meet this condition.
  3. email = NULL does not find those rows: the comparison is unknown, and WHERE only keeps conditions that are true.
  4. 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

Not started

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.

Find the emails that are present

Guided practice

Find the emails that are present

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Combine absence with another condition

Not started

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.

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.

Next: Practice: choose the right filter