← Back to SQL, page 7

sql-0134 · Medium mid

What does CASE dept_id WHEN NULL THEN 'none' ELSE 'has dept' END return for an employee with no department?

It returns 'has dept'. A simple CASE compares with =, and NULL = NULL is unknown, so the WHEN NULL branch never matches. Use a searched CASE with WHEN dept_id IS NULL instead.

Full answer
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, CASE dept_id WHEN NULL THEN 'none' ELSE 'has dept' END AS label
FROM employees WHERE id IN (1, 7) ORDER BY id;
namelabel
Ashahas dept
Lenahas dept

Lena's dept_id is NULL, yet she gets has dept. The simple form CASE expr WHEN value is shorthand for CASE WHEN expr = value, and NULL = NULL is not true.

The searched form fixes it:

sql
SELECT name, CASE WHEN dept_id IS NULL THEN 'none' ELSE 'has dept' END AS label
FROM employees WHERE id IN (1, 7) ORDER BY id;
namelabel
Ashahas dept
Lenanone

PostgreSQL behaves the same way:

sql
SELECT name, CASE dept_id WHEN NULL THEN 'none' ELSE 'has dept' END AS label
FROM employees WHERE id IN (1, 7) ORDER BY id;
namelabel
Ashahas dept
Lenahas dept

The same trap appears with DECODE in Oracle, which is documented to treat NULLs as equal (not run here). That difference is one reason to prefer explicit IS NULL checks everywhere.

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, CASE dept_id WHEN NULL THEN 'none' ELSE 'has dept' END AS label
FROM employees WHERE id IN (1, 7) ORDER BY id;

SELECT name, CASE WHEN dept_id IS NULL THEN 'none' ELSE 'has dept' END AS label
FROM employees WHERE id IN (1, 7) ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Simple CASE compares with =
  • WHEN NULL in a simple CASE never matches
  • Use WHEN col IS NULL in a searched CASE
  • Behaviour is the same in SQLite and PostgreSQL

Common mistakes

  • Writing WHEN NULL in a simple CASE
  • Assuming CASE treats NULLs as equal like GROUP BY does

Follow-ups

  • How does COALESCE relate to CASE?
  • Which other constructs compare with = and miss NULLs?

Tips

  • Rewrite the simple CASE as the searched one to explain