← Back to SQL, page 2

sql-0029 · Easy fresher

Why does WHERE manager_id = NULL return no rows even though some rows have no manager?

Comparing anything with NULL using = gives UNKNOWN, never TRUE, so WHERE filters every row out. Use WHERE manager_id IS NULL. The same applies to <> NULL, which also returns nothing; use IS NOT NULL.

Full answer

This is one of the most common first bugs with NULL:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name FROM employees WHERE manager_id = NULL;
name

(0 rows)

The query runs without an error and returns nothing. For each row, manager_id = NULL asks "is this value equal to an unknown value?". The answer is unknown, and WHERE only keeps rows where the answer is true.

The right test is IS NULL:

sql
SELECT name FROM employees WHERE manager_id IS NULL;
name
Asha

Asha has no manager, because she runs the company. The opposite test fails in the same silent way:

sql
SELECT COUNT(*) AS with_manager_wrong FROM employees WHERE manager_id <> NULL;
with_manager_wrong
0
sql
SELECT COUNT(*) AS with_manager FROM employees WHERE manager_id IS NOT NULL;
with_manager
7

Some engines once offered a setting that made = NULL behave like IS NULL (SQL Server's ANSI_NULLS OFF, now deprecated). Do not rely on anything like that. In application code, watch out for query builders that generate col = ? and then bind a NULL; they need to switch to IS NULL when the value is missing, or use a null-safe operator such as IS NOT DISTINCT FROM.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name FROM employees WHERE manager_id = NULL;

SELECT name FROM employees WHERE manager_id IS NULL;

SELECT COUNT(*) AS with_manager_wrong FROM employees WHERE manager_id <> NULL;

SELECT COUNT(*) AS with_manager FROM employees WHERE manager_id IS NOT NULL;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • = NULL and <> NULL never match
  • Use IS NULL and IS NOT NULL
  • A query that returns nothing without error is the typical symptom
  • Bound parameters with NULL values need IS NULL or a null-safe comparison

Common mistakes

  • Writing col = NULL or col <> NULL
  • Binding NULL into col = ? from application code

Follow-ups

  • What is a null-safe equality operator?
  • How would you count employees with and without a manager in one query?

Tips

  • Mention the silent empty result; it is how this bug is usually found