NULL Values

Updated · 9 min read
easyPostgreSQL 18sqlnullis-nullthree-valued-logicinterview

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

  • NULL means no value. It's not 0, and in PostgreSQL it's not '' (empty text).
  • Find missing values with IS NULL, and present ones with IS NOT NULL.
  • = NULL never matches, because any comparison with NULL is unknown.
  • SQL uses three-valued logic: true, false and unknown. WHERE keeps only true.
  • Arithmetic or || with NULL gives NULL; count(column) skips NULL.

What NULL Means

NULL is a marker for "no value here." The student table shows all three kinds of missing contact details:

SQL▶ Try it
SELECT first_name, email, phone
FROM students
ORDER BY student_id;
Output
 first_name |             email              |     phone
------------+--------------------------------+----------------
 Srihitha   | srihitha.anamaneni@example.com | (415) 555-0142
 Advitha    | NULL                           | (415) 555-0178
 Navika     | NULL                           | NULL
(3 rows)
ValueMeansExample
NULLNo value: unknown or not applicableAdvitha's email
''Text that is known, and empty(none in the Fee Tracker)
0A 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.)

A known zero, a known empty text, and no value at all

IS NULL and IS NOT NULL

To find missing values, use IS NULL. Students with no email:

SQL▶ Try it
SELECT first_name, email
FROM students
WHERE email IS NULL
ORDER BY first_name;
Output
 first_name | email
------------+-------
 Advitha    | NULL
 Navika     | NULL
(2 rows)

And the opposite, students who have a mentor:

SQL▶ Try it
SELECT first_name, mentor_id
FROM students
WHERE mentor_id IS NOT NULL
ORDER BY first_name;
Output
 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:

SQL▶ Try it
SELECT NULL = NULL AS equals, NULL <> NULL AS not_equals, NULL IS NULL AS is_null;
Output
 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:

SQL▶ Try it
SELECT first_name
FROM students
WHERE email = NULL;
Output
 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:

ConditionPayment 5 givesPayment 5 kept?
method = 'card'unknownNo
method <> 'card'unknownNo
NOT method = 'card'unknownNo
method IS NULLtrueYes
method <> 'card' OR method IS NULLtrueYes
true, false or unknown; WHERE keeps only true

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:

SQL▶ Try it
SELECT 5 + NULL AS plus, NULL * 0 AS times_zero, 'a' || NULL AS joined;
Output
 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.

SQL▶ Try it
SELECT count(*) AS students, count(email) AS with_email, count(*) - count(email) AS missing_email
FROM students;
Output
 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.

Arithmetic with NULL gives NULL; count(column) skips it

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:

SQL▶ Try it
UPDATE payments
SET method = 'bitcoin'
WHERE payment_id = 1;
Output
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.

WhereKeeps or allows the row when the condition isSo NULL
WHEREtrueIs left out
CHECKtrue 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
SQL▶ Try it
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;
Output
 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.

Quick check: NULL values

4 questions
1Which condition finds students with no email?
2What does SELECT 5 + NULL; return?
3Advitha and Navika have no email. What does count(email) return for the three students?
4Why can payment 5 have a NULL method despite CHECK (method IN ('card', 'check', 'cash'))?

Key Takeaways

You want toWrite
Rows with a missing valueWHERE email IS NULL
Rows with a valueWHERE email IS NOT NULL
Include missing values in a "not" testWHERE method <> 'card' OR method IS NULL
Count missing valuescount(*) - count(email)
Compare, treating NULLs as equala IS NOT DISTINCT FROM b

Was this tutorial helpful?

Lesson 20 of 115