Stop 03 · Filtering DataTopics · 7/8

NULL Values and IS NULL

Part of the SQL Roadmap

In short

Real data has gaps: a customer who didn't give a phone number, an order not yet delivered. SQL marks these with NULL, and NULL behaves unlike any other value.

NULL means a value is missing or unknown; it is not zero or an empty string. You test for it with IS NULL or IS NOT NULL, because = NULL never matches.

The jaadu holds up two exam answer sheets side by side, one with a big red zero and one with a completely empty box.

Think of it like this

Blank vs zero on an exam sheet

A student who wrote nothing in Question 5 left it blank. That's different from writing an answer and scoring zero. Blank means "no answer recorded". NULL is the blank: not zero, not an empty string, just unknown.

Watch it happen

NULL is not zero or empty

NULL

  • Value is missing or unknown
  • Found with IS NULL, never = NULL
  • Skipped by SUM, AVG and COUNT(column)
  • Any maths with it gives NULL

0 or ''

  • A real, known value
  • Found with = 0 or = ''
  • Counted and added like any value
  • Maths works normally

A delivery fee of 0 means free delivery. A delivery fee of NULL means nobody recorded it. Treat them differently.

The big ideas

What you need to know

NULL means unknown

It isn't a value at all. It's a marker that the value is missing.

Test with IS NULL

WHERE phone IS NULL finds missing phones. WHERE phone = NULL matches nothing, ever.

IS NOT NULL for present values

WHERE delivered_at IS NOT NULL finds orders that have been delivered.

NULL spreads through maths

price + NULL is NULL. 'Hi ' || NULL is NULL in PostgreSQL. COALESCE gives a default.

See it in code

Walk through the code

sql
1-- Customers with no phone recorded2SELECT customer_id, name3FROM customers4WHERE phone IS NULL;5 6-- Orders that have been delivered7SELECT order_id, delivered_at8FROM orders9WHERE delivered_at IS NOT NULL;10 11-- This returns nothing, even if phones are missing12SELECT customer_id FROM customers WHERE phone = NULL;

1/3The right way to find missing values.

IS NULL and IS NOT NULL work identically in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

WHERE discount = NULL.

Do this instead

WHERE discount IS NULL. Comparing anything to NULL with = gives unknown.

Oops

Treating empty strings and NULL as the same.

Do this instead

'' is a known empty value. Check both if your data mixes them: WHERE phone IS NULL OR phone = ''.

Your mission

Find the gaps

~15 min

Quick check

Which query finds orders with no delivery date?

Go deeper

  1. 1Count how many rows have a NULL in an optional column such as phone or discount.
  2. 2Compare WHERE col IS NULL with WHERE col = '' on a text column.
  3. 3Notice how NULLs behave in arithmetic: any value plus NULL gives NULL.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge