← Back to SQL, page 2

sql-0028 · Easy fresher

What is NULL in SQL, and how does three-valued logic change the way conditions are evaluated?

NULL means a value is missing or unknown; it is not zero and not an empty string. Any comparison with NULL gives UNKNOWN instead of true or false, and WHERE keeps only rows where the condition is true. AND and OR follow a three-valued truth table: FALSE AND UNKNOWN is FALSE, TRUE OR UNKNOWN is TRUE, and most other combinations stay UNKNOWN.

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:

sql
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_nullnull_gtis_nullis_not_null
NULLNULL10

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:

sql
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_unknowntrue_and_unknowntrue_or_unknownfalse_or_unknownnot_unknown
0NULL1NULLNULL

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:

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 (SELECT COUNT(*) FROM employees WHERE salary >  60000) AS high,
       (SELECT COUNT(*) FROM employees WHERE salary <= 60000) AS low,
       (SELECT COUNT(*) FROM employees)                       AS total;
highlowtotal
528

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.

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 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