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;
| equal | is_null |
|---|---|
| NULL | true |
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_name | last_name |
|---|---|
| Jin | Singh |
| Mina | Lindqvist |
| Kenji | Osei |
| Daniel | Kim |
| Tessa | Santos |
| Yusuf | Chen |
| Elena | Haddad |
| Greta | Yamamoto |
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_rows | with_email | missing_email |
|---|---|---|
| 120 | 111 | 9 |
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_null | avg_null_as_zero |
|---|---|
| 2.45 | 1.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_name | last_name | |
|---|---|---|
| Liam | Lindqvist | liam.lindqvist@students.northbridge.example |
| Nadia | Santos | no email on file |
| Yuki | Johansson | yuki.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_name | last_name | gpa |
|---|---|---|
| Hiro | Kim | NULL |
| Noor | García | 4.00 |
| Kenji | Silva | 3.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 JOINfound 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