Full answer
NULL is a marker that says "no value here". An employee's salary might not be agreed yet; a student might not have a city on file. NULL is different from 0 and from '', both of which are real values.
Because NULL is unknown, comparing it with anything gives a third logical result, UNKNOWN. Is an unknown salary greater than 50000? Nobody can say. SQLite shows UNKNOWN as NULL:
SELECT NULL = NULL AS null_eq_null,
NULL > 50000 AS null_gt,
NULL IS NULL AS is_null,
NULL IS NOT NULL AS is_not_null;| null_eq_null | null_gt | is_null | is_not_null |
|---|---|---|---|
| NULL | NULL | 1 | 0 |
NULL = NULL is unknown too: two missing values are not known to be equal. To test for missing values, SQL has IS NULL and IS NOT NULL, which always return true or false.
AND and OR follow simple reasoning. If one side of AND is false, the whole thing is false whatever the unknown side is. If one side of OR is true, the whole thing is true. Otherwise the result stays unknown:
SELECT (1 = 0) AND NULL AS false_and_unknown,
(1 = 1) AND NULL AS true_and_unknown,
(1 = 1) OR NULL AS true_or_unknown,
(1 = 0) OR NULL AS false_or_unknown,
NOT NULL AS not_unknown;| false_and_unknown | true_and_unknown | true_or_unknown | false_or_unknown | not_unknown |
|---|---|---|---|---|
| 0 | NULL | 1 | NULL | NULL |
WHERE keeps a row only when the condition is TRUE. Unknown is treated like false for filtering, which is why rows with NULL silently drop out of both salary > 60000 and salary <= 60000:
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 (SELECT COUNT(*) FROM employees WHERE salary > 60000) AS high,
(SELECT COUNT(*) FROM employees WHERE salary <= 60000) AS low,
(SELECT COUNT(*) FROM employees) AS total;| high | low | total |
|---|---|---|
| 5 | 2 | 8 |
Seven of eight employees appear in one bucket or the other. Lena's NULL salary is in neither, so two counts that look as if they should add up to the table size do not.
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 NULL = NULL AS null_eq_null,
NULL > 50000 AS null_gt,
NULL IS NULL AS is_null,
NULL IS NOT NULL AS is_not_null;
SELECT (1 = 0) AND NULL AS false_and_unknown,
(1 = 1) AND NULL AS true_and_unknown,
(1 = 1) OR NULL AS true_or_unknown,
(1 = 0) OR NULL AS false_or_unknown,
NOT NULL AS not_unknown;
SELECT (SELECT COUNT(*) FROM employees WHERE salary > 60000) AS high,
(SELECT COUNT(*) FROM employees WHERE salary <= 60000) AS low,
(SELECT COUNT(*) FROM employees) AS total;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- NULL means unknown or missing, not 0 or ''
- Comparisons with NULL return UNKNOWN
- WHERE keeps only TRUE rows, so UNKNOWN rows drop out
- FALSE AND UNKNOWN is FALSE; TRUE OR UNKNOWN is TRUE
- Use IS NULL and IS NOT NULL to test for NULL
Common mistakes
- Expecting a condition and its opposite to cover every row
- Treating NULL as zero in comparisons
Follow-ups
- Why does NOT IN behave strangely when the list contains NULL?
- What does CHECK (salary > 0) do with a NULL salary?
Tips
- Use the two-bucket count example; it shows the effect in numbers