Sub topic 10
Handling NULL with IS NULL and IS NOT NULL
This chapter assumes you can write a basic SELECT ... FROM ... WHERE query and combine conditions with AND, OR, and NOT. It uses the employees table from earlier chapters. You need access to PostgreSQL, MySQL, or SQLite to run the examples.
What NULL means
NULL is a marker meaning "no value is stored here." It is not zero, not an empty string, and not the text 'NULL'. A column holds NULL when the value is unknown, does not apply, or was never entered.
In the employees table, manager_id is NULL for the person at the top of the reporting chain, because that person has no manager. NULL is a state of the cell, not a value you can compare against.
Here is a small sample used throughout this chapter:
| employee_id | first_name | last_name | department | salary | manager_id |
|---|---|---|---|---|---|
| 1 | Dana | Reyes | Executive | 120000 | NULL |
| 2 | Priya | Nair | Sales | 60000 | 1 |
| 3 | Tom | McBride | Sales | NULL | 2 |
| 4 | Lena | Fischer | Support | 45000 | 2 |
| 5 | Omar | Haddad | NULL | 52000 | 1 |
Dana has no manager. Tom has no recorded salary. Omar has no department.
Why `= NULL` returns nothing
A beginner's first attempt is often this:
SELECT first_name, last_name
FROM employees
WHERE manager_id = NULL;This query runs without error and returns zero rows, even though Dana's manager_id is NULL. The reason is that any comparison with NULL produces neither true nor false. It produces a third result, called unknown. WHERE keeps a row only when its condition is true, so rows whose condition is unknown are dropped, just like rows where it is false.
The same thing happens with every comparison operator:
manager_id = NULLis unknown.manager_id <> NULLis unknown.manager_id > 1is unknown whenmanager_idisNULL.NULL = NULLis unknown, not true.
The last one surprises people. Two unknowns are not known to be equal.
The correct tests: IS NULL and IS NOT NULL
SQL provides dedicated predicates that return plain true or false and never unknown.
SELECT first_name, last_name
FROM employees
WHERE manager_id IS NULL;Finding rows with no manager works because IS NULL checks whether the cell is empty instead of comparing values. With the sample data, this returns Dana Reyes. Writing manager_id = NULL instead would match nothing.
To find rows that do have a value, use IS NOT NULL:
SELECT first_name, last_name, manager_id
FROM employees
WHERE manager_id IS NOT NULL;This returns Priya, Tom, Lena, and Omar. You may also see NOT manager_id IS NULL, which means the same thing, but IS NOT NULL reads more naturally and is the standard form.
Three-valued logic
Because comparisons can return unknown, SQL conditions use three-valued logic: true, false, and unknown. You need to know how AND, OR, and NOT behave with the third value.
| A | B | A AND B | A OR B |
|---|---|---|---|
| TRUE | UNKNOWN | UNKNOWN | TRUE |
| FALSE | UNKNOWN | FALSE | UNKNOWN |
| UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
NOT UNKNOWN is also unknown. The practical rules are:
TRUE OR UNKNOWNis true, so one satisfied branch is enough.FALSE AND UNKNOWNis false, so one failed branch settles the result.- Every other combination that involves unknown stays unknown, and
WHEREdiscards the row.
The trap with NOT and inequality
The most common real-world bug with NULL is a filter that silently drops rows. Consider this query, intended to list everyone outside Sales:
SELECT first_name, department
FROM employees
WHERE department <> 'Sales';You might expect Dana, Lena, and Omar. You get only Dana and Lena. Omar's department is NULL, so NULL <> 'Sales' is unknown, and his row is discarded.
If you want rows with a missing department included, say so explicitly:
SELECT first_name, department
FROM employees
WHERE department <> 'Sales'
OR department IS NULL;Now Omar appears. The same issue affects NOT IN, NOT LIKE, and NOT BETWEEN. Each one produces unknown for a NULL input, so the row disappears.
SELECT first_name, department
FROM employees
WHERE department NOT IN ('Sales', 'Support');This returns Dana only. Omar is missing again, because NULL NOT IN (...) is unknown.
NULL in expressions
NULL spreads through arithmetic. Any calculation with a NULL operand yields NULL:
SELECT first_name, salary * 12 AS annual_salary
FROM employees;Tom's annual_salary is NULL, not zero. Likewise, salary + 1000 is NULL for Tom.
To substitute a default for display or calculation, use COALESCE, which returns its first non-NULL argument. It is part of standard SQL and works in PostgreSQL, MySQL, and SQLite.
SELECT first_name,
COALESCE(salary, 0) * 12 AS annual_salary,
COALESCE(department, 'Unassigned') AS department
FROM employees;Be careful when choosing a default. Replacing a missing salary with 0 is correct for display but can distort an average, since a missing value and a zero salary mean different things.
Concatenation behaves differently between databases. In PostgreSQL and standard SQL, 'a' || NULL is NULL. In MySQL, CONCAT('a', NULL) is also NULL, but CONCAT_WS skips NULL arguments. Check your dialect when building strings from nullable columns.
Testing NULL with other conditions
You can combine IS NULL and IS NOT NULL with the logical operators from the previous chapter. This query finds employees who have a manager but no recorded salary:
SELECT first_name, last_name
FROM employees
WHERE manager_id IS NOT NULL
AND salary IS NULL;With the sample data, this returns Tom McBride.
Aggregate functions deserve a brief mention, although they are covered later in the tutorial. COUNT(*) counts rows, while COUNT(salary) counts only rows where salary is not NULL. Functions such as AVG and SUM ignore NULL values, so Tom's missing salary does not pull the average toward zero.
Dialect notes
IS NULLandIS NOT NULLwork the same in every major database.- PostgreSQL and standard SQL also support
IS DISTINCT FROMandIS NOT DISTINCT FROM, which treat twoNULLs as equal and never return unknown. For example,manager_id IS NOT DISTINCT FROM 1is false for aNULLmanager_id, rather than unknown. SQLite offers the same behavior throughISandIS NOT, and MySQL provides the null-safe equality operator<=>. - Oracle treats an empty string
''asNULL. Most other databases treat them as different values, so a column can hold''and still passIS NOT NULLelsewhere.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| `WHERE col = NULL` | Returns no rows | `WHERE col IS NULL` |
| `WHERE col <> 'x'` on a nullable column | Rows with `NULL` are dropped | Add `OR col IS NULL` |
| `WHERE col NOT IN (subquery)` where the subquery can return `NULL` | Returns no rows | Filter the subquery with `IS NOT NULL` |
| Treating `''` or `0` as missing | Misses real `NULL` rows | Test with `IS NULL` explicitly |
| Using `NULL` in arithmetic | Result is `NULL` | Wrap with `COALESCE` |
Exercises
Exercise
Using the sample table, you run SELECT first_name FROM employees WHERE salary <> 60000;. Which employees are returned, and why is Tom McBride not among them?
Show answer
Dana (120000), Lena (45000), and Omar (52000) are returned. Priya is excluded because her salary equals 60000. Tom is excluded because his salary is NULL, so NULL <> 60000 is unknown, and WHERE keeps only rows where the condition is true.
Exercise
Write a query that lists the first name and department of every employee whose department is either missing or not 'Sales'.
Show answer
SELECT first_name, department FROM employees WHERE department IS NULL OR department <> 'Sales'; This returns Dana, Lena, and Omar. The explicit IS NULL test brings back Omar, whom department <> 'Sales' alone would drop.
Exercise
What does SELECT first_name FROM employees WHERE manager_id = manager_id; return when some rows have a NULL manager_id, and why?
Show answer
It returns only the rows where managerid is not NULL. For a NULL managerid the comparison NULL = NULL is unknown, so those rows are discarded. The condition behaves like manager_id IS NOT NULL.