Skip to content
Hands-on Mentor
ENTR
Back to blog

SQL NULL explained: IS NULL, COALESCE and why = NULL does not work

In SQL, NULL means unknown, so = NULL never matches. Learn IS NULL, COALESCE and how COUNT and AVG treat NULL, with real query results.

4 min read

In SQL, NULL means "unknown" or "no value", and it is not equal to anything, including another NULL. That is why WHERE column = NULL never finds a row. To test for a missing value, write WHERE column IS NULL. Use COALESCE to replace NULL with a default value in your output.

The examples use the student table of the Northbridge University database in Port Alder. It has 120 rows. Some columns have gaps: 8 students have no advisor_id, 9 have no email, and 32 have no gpa yet.

Why does = NULL not work?

Comparing anything with an unknown value gives an unknown answer, and WHERE keeps only rows where the answer is true. You can see it directly:

SELECT NULL = NULL AS equal, NULL IS NULL AS is_null;
equalis_null
NULLtrue

NULL = NULL is NULL, not true. So this query returns no rows, even though 8 students have no advisor:

SELECT first_name, last_name
FROM student
WHERE advisor_id = NULL;

It runs without an error and returns an empty result. This quiet failure is why the mistake is so common.

What is the difference between IS NULL and = NULL?

IS NULL is the correct test. IS NOT NULL is its opposite.

SELECT first_name, last_name
FROM student
WHERE advisor_id IS NULL
ORDER BY id;
first_namelast_name
JinSingh
MinaLindqvist
KenjiOsei
DanielKim
TessaSantos
YusufChen
ElenaHaddad
GretaYamamoto

Why does <> leave NULL rows out too?

Any comparison with NULL is unknown, including <>. Count the students whose advisor is not instructor 7:

SELECT COUNT(*) FROM student WHERE advisor_id <> 7;

The result is 110. Two students have advisor 7, so you might expect 118. The 8 students with no advisor are missing, because NULL <> 7 is unknown. To include them, ask for them explicitly:

SELECT COUNT(*) FROM student WHERE advisor_id <> 7 OR advisor_id IS NULL;

This returns 118. The same rule applies to NOT, <, > and LIKE.

How do you count NULL values in SQL?

COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL. The difference is the number of missing values:

SELECT COUNT(*) AS all_rows,
       COUNT(email) AS with_email,
       COUNT(*) - COUNT(email) AS missing_email
FROM student;
all_rowswith_emailmissing_email
1201119

WHERE email IS NULL gives the same count of 9 and also lets you see the rows.

How do aggregate functions treat NULL?

SUM, AVG, MIN, MAX and COUNT(column) all skip NULL. The average GPA below is computed from the 88 students who have a GPA, not from all 120:

SELECT ROUND(AVG(gpa), 2) AS avg_skip_null,
       ROUND(AVG(COALESCE(gpa, 0)), 2) AS avg_null_as_zero
FROM student;
avg_skip_nullavg_null_as_zero
2.451.80

Treating a missing GPA as zero would pull the average down to 1.80, which is wrong. A missing GPA means "not recorded yet", not "failed everything". Think about what a gap means before you replace it.

How does COALESCE work in SQL?

COALESCE(a, b, c) returns the first value in the list that is not NULL. Use it to show a readable default:

SELECT first_name, last_name,
       COALESCE(email, 'no email on file') AS email
FROM student
WHERE id IN (11, 12, 13)
ORDER BY id;
first_namelast_nameemail
LiamLindqvistliam.lindqvist@students.northbridge.example
NadiaSantosno email on file
YukiJohanssonyuki.johansson@students.northbridge.example

COALESCE changes only the output. The stored data stays NULL.

Where does NULL go when you sort?

In PostgreSQL, NULL sorts as the largest value. With ORDER BY gpa DESC, the missing GPAs come first:

SELECT first_name, last_name, gpa
FROM student
WHERE year = 2
ORDER BY gpa DESC
LIMIT 3;
first_namelast_namegpa
HiroKimNULL
NoorGarcía4.00
KenjiSilva3.83

Add NULLS LAST to put them at the end and see the top GPAs first:

SELECT first_name, last_name, gpa
FROM student
WHERE year = 2
ORDER BY gpa DESC NULLS LAST
LIMIT 3;

The top row is now Noor García (4.00). Other database systems may sort NULL differently, so add NULLS FIRST or NULLS LAST when the order matters.

Why are values NULL in the first place?

Three common reasons:

  • The value is not known yet, such as a GPA for a student who has no completed exams.
  • The value does not apply, such as an advisor for a student who has not been assigned one.
  • A LEFT JOIN found no matching row. See [SQL joins explained](/blog/sql-join-types).

What to read next

  • Practice in your browser: [NULL values](/learn/sql/null-values) and [Case classification](/learn/sql/case-classification)
  • Related post: [SQL CASE WHEN](/blog/sql-case-when) turns NULL checks into labels
  • Group data and count it: [SQL GROUP BY explained](/blog/sql-group-by)
  • Browse the whole course: [SQL course](/courses/sql)
  • #sql
  • #null
  • #coalesce
  • #beginners
All posts