In SQL, NULL means a value is missing or unknown. It isn’t zero, an empty string, or false, and it behaves differently from every other value: any comparison with NULL, even NULL = NULL, returns “unknown” instead of true or false. Use IS NULL to find it, COALESCE to replace it, and NULLIF to create it on purpose.
NULL causes more confusing SQL bugs than anything else. Rows silently disappear from results, counts don’t match, and a filter that looks correct returns nothing at all. Each earlier part of the Learn SQL series ran into NULL at some point; this part explains how it works, so those surprises make sense.
The examples use the series’ sample database: download shop.sql and load it as shown in Part 1. In it, Daniel has no email and Laura has no city. Every example was tested on PostgreSQL, MySQL, SQL Server, and SQLite.
What NULL Means
NULL marks the absence of a value. It can mean:
- Unknown: we don’t know Laura’s city.
- Not applicable: a cancelled order has no ship date.
- Not provided yet: a customer skipped the optional phone field.
That’s different from a real value:
| Value | Meaning |
|---|---|
NULL | No value |
0 | The number zero |
'' | Text that exists but is empty |
FALSE | A known “no” |
Whether a column can hold NULL is decided when the table is created: columns are nullable unless they’re declared NOT NULL. See Creating Tables in SQL.
Why = NULL Doesn’t Work: Three-Valued Logic
SQL conditions don’t just return true or false. They can also return unknown, and that’s what any comparison with NULL produces:
SELECT NULL = NULL AS equal,
NULL <> NULL AS not_equal,
NULL IS NULL AS is_null,
1 + NULL AS math,
'a' || NULL AS text;
equal | not_equal | is_null | math | text
--------+-----------+---------+--------+--------
(null) | (null) | t | (null) | (null)
Read it as: “Is an unknown value equal to another unknown value? Unknown.” Arithmetic and text concatenation with NULL also return NULL. Only IS NULL gives a real answer.
WHERE keeps a row only when its condition is true. Unknown counts as “not true,” so the row is dropped. That’s why WHERE email = NULL returns nothing.
AND, OR, and NOT follow these rules when one side is unknown:
| Expression | Result |
|---|---|
TRUE AND NULL | NULL (unknown) |
FALSE AND NULL | FALSE |
TRUE OR NULL | TRUE |
FALSE OR NULL | NULL (unknown) |
NOT NULL | NULL (unknown) |
FALSE AND anything is false, and TRUE OR anything is true, so those cases still have a definite answer.
Finding NULLs: IS NULL and IS NOT NULL
SELECT first_name, last_name FROM customers WHERE email IS NULL; -- Daniel
SELECT COUNT(*) FROM customers WHERE email IS NOT NULL; -- 7
SQL SELECT Basics introduced these. Here’s the case that catches people.
The Disappearing-Rows Trap
Find every customer who doesn’t live in San Diego:
SELECT first_name, city
FROM customers
WHERE city <> 'San Diego'
ORDER BY first_name;
first_name | city
------------+-----------
Ana | El Centro
Daniel | Yuma
Kevin | Las Vegas
Maria | El Centro
Sofia | Phoenix
Laura is missing. Her city is NULL, so NULL <> 'San Diego' is unknown, and the row is dropped. If she should count as “not San Diego,” include NULLs explicitly:
SELECT first_name, city
FROM customers
WHERE city <> 'San Diego' OR city IS NULL
ORDER BY first_name;
first_name | city
------------+-----------
Ana | El Centro
Daniel | Yuma
Kevin | Las Vegas
Laura | (null)
Maria | El Centro
Sofia | Phoenix
(The (null) label comes from a psql display setting, \pset null '(null)'; by default NULLs show as blanks.)
NULL-Safe Comparisons
Several databases have an operator that treats two NULLs as equal and a NULL as different from any value, so you don’t need the extra OR ... IS NULL:
| Database | “Not equal, treating NULL as a value” | “Equal, treating NULL as a value” |
|---|---|---|
| PostgreSQL, SQL Server 2022 | city IS DISTINCT FROM 'San Diego' | IS NOT DISTINCT FROM |
| MySQL | NOT (city <=> 'San Diego') | city <=> 'San Diego' |
| SQLite | city IS NOT 'San Diego' | city IS 'San Diego' |
Each returns the same six customers, including Laura.
Replacing NULLs: COALESCE
COALESCE returns the first value in its list that isn’t NULL. It’s the standard way to show a fallback:
SELECT first_name,
COALESCE(email, 'no email on file') AS email,
COALESCE(city, 'Unknown') AS city
FROM customers
WHERE email IS NULL OR city IS NULL
ORDER BY first_name;
first_name | email | city
------------+------------------------+---------
Daniel | no email on file | Yuma
Laura | [email protected] | Unknown
It accepts any number of arguments, like COALESCE(mobile_phone, work_phone, home_phone, 'none'). COALESCE works in every database; some also have two-argument shortcuts: IFNULL (MySQL, SQLite), ISNULL (SQL Server), and NVL (Oracle).
Use COALESCE in the SELECT for display, but think before using it in calculations. Replacing a missing discount with 0 changes the meaning of an average; see the NULL section of SQL Aggregate Functions.
Creating NULLs on Purpose: NULLIF
NULLIF(a, b) returns NULL if a equals b, and a otherwise. Two common uses:
Avoid division-by-zero errors. Dividing by NULL returns NULL instead of crashing the query:
SELECT 10 / NULLIF(0, 0) AS safe_divide; -- NULL, not an error
In a real report, guard the denominator, such as a percentage per order status:
SELECT status,
COUNT(*) AS orders,
ROUND(100.0 * SUM(CASE WHEN quantity > 1 THEN 1 ELSE 0 END)
/ NULLIF(COUNT(*), 0), 1) AS pct_multi_item
FROM orders
GROUP BY status
ORDER BY status;
status | orders | pct_multi_item
-----------+--------+----------------
cancelled | 1 | 100.0
pending | 2 | 50.0
returned | 1 | 0.0
shipped | 8 | 62.5
Turn placeholder values into real NULLs when cleaning data: NULLIF(phone, '') or NULLIF(city, 'N/A'). Then IS NULL, COUNT, and COALESCE all treat them correctly. More techniques are in Cleaning Messy Data with Pure SQL.
NULL in CASE Expressions
Test for NULL with WHEN column IS NULL:
SELECT first_name,
CASE WHEN email IS NULL THEN 'Missing' ELSE 'On file' END AS email_status
FROM customers
ORDER BY customer_id;
Daniel gets “Missing”; everyone else gets “On file.” Don’t use the short form CASE email WHEN NULL THEN ...: it compares with =, which never matches NULL, so Daniel would wrongly get “On file.”
NULL in Aggregates and Grouping
Aggregate functions skip NULLs.
COUNT(*)counts rows, butCOUNT(email)counts only non-NULL values:SELECT COUNT(*) AS all_rows, COUNT(email) AS emails, COUNT(city) AS cities FROM customers;all_rows | emails | cities ----------+--------+-------- 8 | 7 | 7SUM,AVG,MIN, andMAXreturn NULL when there are no rows, so wrap them inCOALESCE(SUM(x), 0)for reports.GROUP BYputs all NULLs in one group, andDISTINCTtreats them as one value:SELECT city, COUNT(*) FROM customers GROUP BY city ORDER BY city;city | count -----------+------- El Centro | 2 Las Vegas | 1 Phoenix | 1 San Diego | 2 Yuma | 1 (null) | 1ORDER BYplaces NULLs first or last depending on the database; see SQL SELECT Basics.
NULL in Joins: Finding Missing Matches
A LEFT JOIN keeps every row from the left table and fills in NULL where there’s no match on the right. That makes NULL useful: it’s how you find rows without a match. Which customers have never ordered?
SELECT c.first_name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
first_name | order_id
------------+----------
Laura | (null)
And when counting orders per customer, COUNT(o.order_id) correctly gives Laura 0, while COUNT(*) would give her 1 (her single row with NULLs). See SQL JOINs Explained for more on outer joins.
The NOT IN Trap
This is the most dangerous NULL bug, because the query looks right and returns no rows at all.
Which California customers live in a city that has no Nevada customers? The Nevada cities are Las Vegas and Laura’s NULL:
SELECT first_name, city
FROM customers
WHERE state = 'CA'
AND city NOT IN (SELECT city FROM customers WHERE state = 'NV');
first_name | city
------------+------
Empty, even though no California customer lives in Las Vegas. Here’s why: city NOT IN ('Las Vegas', NULL) means city <> 'Las Vegas' AND city <> NULL. The second part is always unknown, so the whole condition is never true. This happens in all four databases.
Two fixes:
-- 1. Use NOT EXISTS (recommended): NULLs can't break it
SELECT first_name, city
FROM customers c
WHERE state = 'CA'
AND NOT EXISTS (
SELECT 1 FROM customers n
WHERE n.state = 'NV' AND n.city = c.city
)
ORDER BY first_name;
-- 2. Or remove NULLs from the subquery
SELECT first_name, city
FROM customers
WHERE state = 'CA'
AND city NOT IN (SELECT city FROM customers WHERE state = 'NV' AND city IS NOT NULL)
ORDER BY first_name;
first_name | city
------------+-----------
Ana | El Centro
James | San Diego
Maria | El Centro
Omar | San Diego
Both return the four California customers. Make NOT EXISTS your default for “not in another table” questions. Mastering SQL Subqueries covers EXISTS in more depth.
NULL and UNIQUE Constraints
Can a UNIQUE column hold more than one NULL? It depends:
| Database | Multiple NULLs in a UNIQUE column |
|---|---|
| PostgreSQL, MySQL, SQLite | Allowed (NULLs aren’t considered equal) |
| SQL Server | Only one NULL allowed |
In SQL Server, the second NULL fails with Violation of UNIQUE KEY constraint. If you need “unique when present” there, use a filtered unique index: CREATE UNIQUE INDEX ... WHERE email IS NOT NULL. PostgreSQL 15 and later can go the other way and treat NULLs as equal with UNIQUE NULLS NOT DISTINCT.
When Should a Column Allow NULL?
- Allow NULL when a value can genuinely be unknown or not apply, such as an optional middle name, a ship date for an order that hasn’t shipped, or a phone number a customer chose not to give.
- Use
NOT NULLwhen a value is always required, such as IDs, names, prices, and order dates. It prevents a whole category of bugs. - Don’t use placeholder values instead of NULL, like
0for an unknown price or'1900-01-01'for an unknown date. They look like real data and quietly distort averages and filters. - Don’t confuse NULL with an empty string. Pick one meaning for “no value” in each column (NULL is usually better) and stick with it. Oracle is a special case: it treats
''as NULL.
Common Mistakes to Avoid
- Writing
= NULLor<> NULLinstead ofIS NULLandIS NOT NULL. - Forgetting that
<>andNOT INdrop NULL rows. - Using
NOT INwith a subquery that can return NULL. UseNOT EXISTS. - Using
COUNT(column)when you meanCOUNT(*), or the reverse. - Letting NULL spread through math and text.
price * NULLand'a' || NULLare NULL; useCOALESCEwhere a default makes sense. - Using
CASE column WHEN NULL, which never matches. - Assuming SQL Server allows several NULLs in a UNIQUE column.
Frequently Asked Questions
What does NULL mean in SQL?
That a value is missing or unknown. It’s not zero, an empty string, or false.
Why doesn’t WHERE column = NULL work?
Any comparison with NULL returns “unknown,” not true, and WHERE only keeps rows where the condition is true. Use WHERE column IS NULL.
What’s the difference between COALESCE and ISNULL?
COALESCE is standard SQL, works in every database, and takes any number of arguments. ISNULL is SQL Server’s two-argument version (in MySQL, ISNULL(x) instead tests whether x is NULL). Prefer COALESCE for portable SQL.
Is NULL the same as an empty string?
No. An empty string is a known value with no characters; NULL means there’s no value at all. Oracle is the exception, treating '' as NULL.
Does COUNT include NULL values?
COUNT(*) counts every row, including rows with NULLs. COUNT(column) skips rows where that column is NULL.
How do I replace NULL with 0 in SQL?
Use COALESCE(column, 0). It returns the column’s value when it exists and 0 when it’s NULL.
Conclusion
NULL means “unknown,” and SQL treats unknowns cautiously: comparisons with NULL are never true, so rows quietly drop out of WHERE, <>, and NOT IN filters. Use IS NULL and IS NOT NULL to test for it, COALESCE to supply a fallback, NULLIF to avoid division by zero, LEFT JOIN ... IS NULL or NOT EXISTS to find missing matches, and NOT NULL constraints to keep NULLs out of columns that must always have a value.
This completes Level 1: Foundations of the Learn SQL series. You can now read, filter, sort, summarize, join, create, and change data, and you know where NULL hides. Level 2 builds on this with SQL String Functions, date functions, subqueries, and window functions.
