← Back to SQL, page 3

sql-0042 · Easy fresher

A report filters WHERE dept_id <> 2 to exclude Sales. Why does an employee with no department disappear too?

For an employee with a NULL dept_id, dept_id <> 2 is UNKNOWN, not TRUE, so WHERE drops the row. If people with no department should stay, write WHERE dept_id <> 2 OR dept_id IS NULL, or use dept_id IS DISTINCT FROM 2.

Full answer

"Everyone except Sales" sounds like it should include people with no department. SQL disagrees:

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, dept_id FROM employees WHERE dept_id <> 2 ORDER BY name;
namedept_id
Asha1
Diego3
Mei1
Omar3
Ravi1

Lena is missing. Her dept_id is NULL, and NULL <> 2 is UNKNOWN. WHERE only keeps TRUE rows.

If the business meaning is "everyone not known to be in Sales", include the NULL case on purpose:

sql
SELECT name, dept_id FROM employees WHERE dept_id <> 2 OR dept_id IS NULL ORDER BY name;
namedept_id
Asha1
Diego3
LenaNULL
Mei1
Omar3
Ravi1

The null-safe operator says the same thing in one condition:

sql
SELECT name, dept_id FROM employees WHERE dept_id IS DISTINCT FROM 2 ORDER BY name;
namedept_id
Asha1
Diego3
LenaNULL
Mei1
Omar3
Ravi1

This is the mirror image of the NOT IN trap. Any time a filter uses <>, NOT LIKE or NOT IN on a nullable column, ask whether rows with NULL should be in or out, and make the query say it. Silent exclusion is how headcount reports end up with numbers that do not add up.

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, dept_id FROM employees WHERE dept_id <> 2 ORDER BY name;

SELECT name, dept_id FROM employees WHERE dept_id <> 2 OR dept_id IS NULL ORDER BY name;

SELECT name, dept_id FROM employees WHERE dept_id IS DISTINCT FROM 2 ORDER BY name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • col <> value drops rows where col is NULL
  • Add OR col IS NULL when NULL rows should stay
  • IS DISTINCT FROM is the one-condition version
  • Check every negative filter on a nullable column

Common mistakes

  • Assuming 'not equal to X' includes unknowns
  • Reconciling counts and finding they do not add up to the table total

Follow-ups

  • Does NOT LIKE behave the same way with NULL?
  • How would you write the filter on an engine without IS DISTINCT FROM?

Tips

  • State the business question first: should unknowns be included?