IS NULL and IS NOT NULL operators

Concepts covered: Null Handling

SQL has a dedicated test for a missing value. phone IS NULL asks one thing about a cell: was anything recorded here? Unlike a comparison, that question always has a definite answer, so IS NULL returns TRUE or FALSE and never UNKNOWN.

The keyword is IS, because = compares 2 values and a NULL cell has no value to compare.

A shop's customers table has 4 people in it, and the support team wants to know who can't be reached by phone. Play the query to see the test run on each row.

SELECT customer_id, name, phone
FROM customers
WHERE phone IS NULL
customers
customer_idnamephone
1Ann555-0101
2BenNULL
3Cara555-0199
4DevNULL

Ben and Dev come back. For Ann and Cara the test is FALSE, because a phone number is sitting in the cell. For Ben and Dev it is TRUE. Every row got a yes or a no, which is exactly what the = comparison couldn't deliver.

The opposite test

IS NOT NULL flips the answer: TRUE when the cell holds a value, FALSE when it does not. It's the filter for "only rows where this was recorded", such as the customers a courier can call.

SELECT
  customer_id,
  name,
  phone
FROM customers
WHERE phone IS NOT NULL
customers
customer_idnamephone
1Ann555-0101
2BenNULL
3Cara555-0199
4DevNULL
Result
customer_idnamephone
1Ann555-0101
3Cara555-0199

It returns Ann and Cara. 2 rows from IS NULL plus 2 rows from ISN'T NULL make all 4 customers. This pair always splits a table with nothing left over, because each row answers TRUE to exactly one of them. Compare that with = 'US' and != 'US' in the last section, which lost 2 of 5 orders between them.

3 tests on the same 3 cells

The table below runs 3 tests on 3 possible contents of a phone cell. The middle row is the one from the first section: an empty string is a value, so IS NULL doesn't catch it.

phone holdsphone = NULLphone IS NULLphone IS NOT NULL
'555-0101'UNKNOWNFALSETRUE
''UNKNOWNFALSETRUE
NULLUNKNOWNTRUEFALSE

Combining it with other conditions

IS NULL is a condition like any other, so AND can join it to the comparisons you already write. And because it never returns UNKNOWN, it adds no third value to the AND. The finance team wants large orders that still need a region filled in.

SELECT
  order_id,
  amount
FROM orders
WHERE region IS NULL
  AND amount > 250
orders
order_idamountregion
ORD-001100US
ORD-002200NULL
ORD-003150EU
ORD-004300NULL
ORD-005250US
Result
order_idamount
ORD-004300

Only ORD-004 qualifies. ORD-002 also has no region, but its amount of 200 fails the second test, so the AND comes out FALSE.

Auditing a column before you trust it

IS NULL is most useful as a check you run before any analysis, to find out how much of a column is missing. Pair the test with COUNT(*) and the size of the gap comes back as one number.

SELECT
  COUNT(*) AS orders_missing_region
FROM orders
WHERE region IS NULL
orders
order_idamountregion
ORD-001100US
ORD-002200NULL
ORD-003150EU
ORD-004300NULL
ORD-005250US
Result
orders_missing_region
2

2 of 5 orders have no region. That is 40% of the table, and it tells you that any filter on a named region can reach at most 3 of the 5 orders.

The same pair of tools settles the puzzle from the last section. To report every order that isn't confirmed as US, you need 2 filters. region != 'US' finds 1 order with a different region and region IS NULL finds the 2 with no region at all, so the answer is 3. Together with the 2 US orders, all 5 rows are accounted for.

Do
  • Write IS NULL to find rows where a value is missing.
  • Write ISN'T NULL to keep only rows where a value was recorded.
  • Count the NULLs in a column before filtering or grouping on it.
Avoid
  • Write = NULL or != NULL. Both return 0 rows and raise no error.
  • Expect IS NULL to catch empty strings. '' is a value.
  • Assume a filter and its opposite cover every row of the table.

Now build the test yourself, first by choosing the right operator and then by assembling a full query.

A real filter rarely tests for NULL alone. WHERE carries the first condition, and each AND line after it adds one more. The NULL test sits on its own AND line exactly as a comparison would, and a row comes back only when every line is TRUE for it. Build the next query that way: one comparison, one NULL test, joined by AND.

The warehouse team wants the order_id and amount of every order above 150 that has a region recorded. Arrange the tiles into that query. 2 tiles do not belong.

With IS NULL you can find the gaps, and with IS NOT NULL you can step around them. Filters choose which rows a query reads. The next question is what happens when the rows stay in and an aggregate has to count or add a column that has gaps in it.

About this lesson

This section is part of the NULL Values: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.