NULL Values
NULL marks a value that isn't there: unknown, missing, or not applicable. Advitha has no email on file, Navika has no phone, and payment 5 has no recorded method. NULL isn't zero and isn't empty text; it's the absence of a value. That one idea explains every surprise in this lesson: why = NULL finds nothing, why calculations with NULL give NULL, and why IS NULL exists. You've met pieces of this already; here it all comes together.
In One Minute
NULLmeans no value. It's not0, and in PostgreSQL it's not''(empty text).- Find missing values with
IS NULL, and present ones withIS NOT NULL. = NULLnever matches, because any comparison withNULLis unknown.- SQL uses three-valued logic: true, false and unknown.
WHEREkeeps only true. - Arithmetic or
||withNULLgivesNULL;count(column)skipsNULL.
What NULL Means
NULL is a marker for "no value here." The student table shows all three kinds of missing contact details:
SELECT first_name, email, phone
FROM students
ORDER BY student_id; first_name | email | phone
------------+--------------------------------+----------------
Srihitha | srihitha.anamaneni@example.com | (415) 555-0142
Advitha | NULL | (415) 555-0178
Navika | NULL | NULL
(3 rows)| Value | Means | Example |
|---|---|---|
NULL | No value: unknown or not applicable | Advitha's email |
'' | Text that is known, and empty | (none in the Fee Tracker) |
0 | A number that is known, and zero | (a zero balance) |
These are three different things, and PostgreSQL keeps them apart. (Oracle is an exception: it treats empty text as NULL, which is Oracle only.)
IS NULL and IS NOT NULL
To find missing values, use IS NULL. Students with no email:
SELECT first_name, email
FROM students
WHERE email IS NULL
ORDER BY first_name; first_name | email
------------+-------
Advitha | NULL
Navika | NULL
(2 rows)And the opposite, students who have a mentor:
SELECT first_name, mentor_id
FROM students
WHERE mentor_id IS NOT NULL
ORDER BY first_name; first_name | mentor_id
------------+-----------
Advitha | 101
Navika | 102
(2 rows)IS NULL and IS NOT NULL always answer true or false, never unknown, so they're safe in any WHERE.
Why = NULL Never Matches
Comparing anything with NULL gives unknown, even comparing NULL with NULL. "Is this missing email equal to that missing email?" has no honest answer, because we don't know what either one is:
SELECT NULL = NULL AS equals, NULL <> NULL AS not_equals, NULL IS NULL AS is_null; equals | not_equals | is_null
--------+------------+---------
NULL | NULL | t
(1 row)So WHERE email = NULL checks "is email equal to unknown?" for every row, gets unknown every time, and returns nothing, without any error:
SELECT first_name
FROM students
WHERE email = NULL; first_name
------------
(0 rows)Three-Valued Logic
Because of NULL, a condition in SQL can be true, false or unknown. AND, OR and NOT follow fixed rules for unknown, which you saw in AND, OR and NOT: unknown AND false is false, unknown OR true is true, and NOT unknown is still unknown. WHERE keeps a row only when its condition is true, so rows that come out unknown are left out, just like false ones.
That's why a missing value falls through both a test and its opposite. Seven payments, and payment 5 has no method:
| Condition | Payment 5 gives | Payment 5 kept? |
|---|---|---|
method = 'card' | unknown | No |
method <> 'card' | unknown | No |
NOT method = 'card' | unknown | No |
method IS NULL | true | Yes |
method <> 'card' OR method IS NULL | true | Yes |
PostgreSQL also has IS DISTINCT FROM, which treats two NULL values as equal and never gives unknown: method IS DISTINCT FROM 'card' keeps payment 5. It's standard SQL, supported by PostgreSQL and recent SQL Server versions; MySQL writes the opposite test as <=> (MySQL only).
NULL in Calculations
Almost any calculation with NULL gives NULL. If you don't know one side, you don't know the answer:
SELECT 5 + NULL AS plus, NULL * 0 AS times_zero, 'a' || NULL AS joined; plus | times_zero | joined
------+------------+--------
NULL | NULL | NULL
(1 row)Even NULL * 0 is NULL: SQL doesn't reason that "anything times zero is zero" when it doesn't know the value.
SELECT count(*) AS students, count(email) AS with_email, count(*) - count(email) AS missing_email
FROM students; students | with_email | missing_email
----------+------------+---------------
3 | 1 | 2
(1 row)count(*) counts every row; count(email) counts only the rows that have an email. Aggregate Functions covers the others, and NULL Functions shows COALESCE, which replaces a missing value with one you choose.
Under the Hood: Why CHECK Lets NULL Through
A WHERE keeps only true rows, but a CHECK rule rejects only false ones. That asymmetry surprises many people. payments.method has a rule that allows only card, check or cash. A wrong value is refused:
UPDATE payments
SET method = 'bitcoin'
WHERE payment_id = 1;ERROR: new row for relation "payments" violates check constraint "payments_method_check"
DETAIL: Failing row contains (1, 101, 1, 450.00, 2025-09-01, 2025-08-28, bitcoin).Yet payment 5 stores NULL in that same column. For NULL, "is method one of card, check, cash?" is unknown, and a CHECK only rejects a row when its condition is false. Unknown passes. The design is deliberate: a CHECK describes what a value must look like if it exists, and whether a value may be missing at all is a separate rule, NOT NULL. You'll write both in Constraints.
| Where | Keeps or allows the row when the condition is | So NULL |
|---|---|---|
WHERE | true | Is left out |
CHECK | true or unknown (not false) | Is allowed |
Common Mistakes
Writing = NULL or <> NULL. They return no rows. Use IS NULL and IS NOT NULL.
Treating NULL as zero or empty text. NULL + 5 is NULL, not 5, and NULL isn't ''.
Forgetting NULL in "not" conditions. <>, NOT, NOT IN and NOT LIKE all leave out rows where the value is NULL.
Assuming a CHECK rule keeps NULL out. It doesn't; add NOT NULL when a value is required.
Interview Corner
What is the difference between NULL and an empty string?
NULL means no value at all; '' is a known text value with zero characters. In PostgreSQL, SQL Server and MySQL they're different, and '' IS NULL is false. Oracle is the exception: it stores empty text as NULL.
Why does WHERE column = NULL return no rows?
Any comparison with NULL, including NULL = NULL, is unknown, and WHERE keeps only rows where the condition is true. Use IS NULL.
What does SELECT NULL * 0; return?
NULL. Arithmetic with an unknown value has an unknown result, even when multiplying by zero.
Can a column with a CHECK (method IN ('card', 'check', 'cash')) contain NULL?
Yes, unless it's also NOT NULL. For NULL the check is unknown, and a CHECK only rejects rows where it's false.
Practice
Challenge (module checkpoint): Find the students in California who joined in 2025 and have no email address on file. Show first_name, state, joined_on and email, sorted by first_name. Use a half-open date range.
Show solution
SELECT first_name, state, joined_on, email
FROM students
WHERE state = 'CA'
AND joined_on >= '2025-01-01' AND joined_on < '2026-01-01'
AND email IS NULL
ORDER BY first_name; first_name | state | joined_on | email
------------+-------+------------+-------
Advitha | CA | 2025-01-13 | NULL
(1 row)Only Advitha matches all three. Srihitha lives in California but joined in 2024 and has an email; Navika has no email but lives in Texas.
Key Takeaways
| You want to | Write |
|---|---|
| Rows with a missing value | WHERE email IS NULL |
| Rows with a value | WHERE email IS NOT NULL |
| Include missing values in a "not" test | WHERE method <> 'card' OR method IS NULL |
| Count missing values | count(*) - count(email) |
| Compare, treating NULLs as equal | a IS NOT DISTINCT FROM b |
Was this tutorial helpful?