01 / 21
?

IS NULL / IS NOT NULL
Handling Missing Data

Learn how SQL represents missing or unknown values with NULL and how to filter rows using IS NULL and IS NOT NULL.

SQL

What is NULL?

In SQL, NULL represents a missing, unknown, or undefined value. It is not the same as zero, an empty string, or the word "null": it means the data simply does not exist.

NULL
Unknown or missing: no value at all
0 or ''
Known values: zero is a number, '' is an empty string

Why NULL Matters in Databases

Real-world data is messy. Customers might not provide a phone number. An order might not have a shipping date yet. NULL lets a database honestly represent that gap instead of faking it.

name email phone Alice alice@mail.com 555-1234 Bob bob@mail.com NULL Carol NULL 555-9876

SQL Uses Three-Valued Logic

Most programming languages have TRUE and FALSE. SQL adds a third state: UNKNOWN. Any comparison involving NULL produces UNKNOWN, not TRUE or FALSE.

TRUE FALSE UNKNOWN Any comparison with NULL yields UNKNOWN

The Core Rule: IS NULL / IS NOT NULL

-- Find rows WHERE a column has no value
SELECT *
FROM table_name
WHERE column_name IS NULL;

-- Find rows WHERE a column HAS a value
SELECT *
FROM table_name
WHERE column_name IS NOT NULL;

How IS NULL Filters Rows

ALL ROWS name phone Alice 555-1234 Bob NULL Carol 555-9876 Dave NULL WHERE phone IS NULL RESULT name phone Bob NULL Dave NULL IS NULL returns only rows where the value is missing

\(=\) NULL vs IS NULL

Expression Result Returns Rows?
WHERE phone \(=\) NULL UNKNOWN Never
WHERE phone \(\neq\) NULL UNKNOWN Never
WHERE phone IS NULL TRUE / FALSE Yes ✓
WHERE phone IS NOT NULL TRUE / FALSE Yes ✓

NULL Propagates Through Expressions

Any arithmetic or string operation with NULL produces NULL. This is called NULL propagation: the unknown value "infects" the entire result.

\(\color{#f472b6}\text{NULL} + 5\)
Result: NULL
\(\text{'Hi'} \Vert \color{#f472b6}\text{NULL}\)
Result: NULL
\(\color{#f472b6}\text{NULL} = \color{#f472b6}\text{NULL}\)
Result: UNKNOWN

How to Apply NULL Checks

1
Identify the column you need to check for missing data.
2
Choose the operator: use IS NULL to find missing values, IS NOT NULL to find present values.
3
Place it in WHERE: write the condition in the WHERE clause of your query.
4
Combine with AND / OR: you can chain multiple NULL checks together with other conditions.

Example 1: Find Missing Emails

-- Find all students without an email address
SELECT student_id, name
FROM students
WHERE email IS NULL;

Example 2: Complete Records Only

-- Find orders that have been shipped
SELECT order_id, customer, ship_date
FROM orders
WHERE ship_date IS NOT NULL;

Example 3: Combining Conditions

-- Active users who haven't set a profile picture
SELECT username, signup_date
FROM users
WHERE is_active = 1
  AND profile_pic IS NULL;

Replacing NULLs with COALESCE

Sometimes you don't want to filter NULLs out: you want to replace them with a default value. The COALESCE function returns the first non-NULL argument.

-- Show 'N/A' instead of NULL for phone
SELECT name,
       COALESCE(phone, 'N/A') AS phone_display
FROM contacts;
COALESCE
Standard SQL: works in PostgreSQL, MySQL, SQLite, SQL Server, Oracle
IFNULL
MySQL / SQLite shorthand: takes exactly two arguments

NULLs in Aggregate Functions

Aggregate functions like COUNT, SUM, AVG handle NULLs differently. Understanding this prevents subtle bugs in reports.

Function NULL Behavior
COUNT(*) Counts all rows, including NULLs
COUNT(column) Skips NULL values
SUM(column) Ignores NULLs (does not add 0)
AVG(column) Excludes NULLs from count and sum

Common Mistakes with NULL

✕ Using \(=\) NULL
Writing WHERE x \(=\) NULL always evaluates to UNKNOWN: no rows are ever returned.
✕ Using \(\neq\) NULL
Writing WHERE x \(\neq\) NULL also evaluates to UNKNOWN: it does not find non-NULL values.
✕ Assuming \(\neq\) Includes NULLs
A condition like status \(\neq\) 'closed' will exclude rows where status is NULL. To also include these, you must write WHERE status \(\neq\) 'closed' OR status IS NULL.

Quick Reference Summary

Task SQL Syntax Notes
Find missing values WHERE col IS NULL Returns rows with no data
Find present values WHERE col IS NOT NULL Excludes missing data
Replace NULLs COALESCE(col, default) Standard SQL function
Count non-NULLs COUNT(col) Skips NULL automatically
Count all rows COUNT(*) Includes NULL rows

Question 1 of 5

Which SQL expression correctly finds rows where the city column has no value?

Question 2 of 5

What does NULL \(+\) 10 evaluate to in SQL?

Question 3 of 5

The orders table has 100 rows. The ship_date column is NULL for 30 rows. What does SELECT COUNT(ship_date) FROM orders return?

Question 4 of 5

What does COALESCE(NULL, NULL, 'hello', 'world') return?

Question 5 of 5

Which query finds active users who do have a phone number on file?