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;| name | dept_id |
|---|---|
| Asha | 1 |
| Diego | 3 |
| Mei | 1 |
| Omar | 3 |
| Ravi | 1 |
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;| name | dept_id |
|---|---|
| Asha | 1 |
| Diego | 3 |
| Lena | NULL |
| Mei | 1 |
| Omar | 3 |
| Ravi | 1 |
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;| name | dept_id |
|---|---|
| Asha | 1 |
| Diego | 3 |
| Lena | NULL |
| Mei | 1 |
| Omar | 3 |
| Ravi | 1 |
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?