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.
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.
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.
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.
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
\(=\) 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.
How to Apply NULL Checks
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;
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
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?
The users table has columns: user_id, name, phone, and is_active (1 for active, 0 for inactive).