Sub topic 2
Three-Valued Logic: TRUE, FALSE, and UNKNOWN
This chapter assumes you can write SELECT, WHERE, and ORDER BY queries and read basic boolean expressions such as a > 5 AND b = 2. It also assumes you have read the previous chapter on what NULL means. Here you learn the rule that decides how every NULL-related surprise behaves: SQL conditions have three possible results, not two.
Why Two Values Are Not Enough
In most programming languages a condition is either true or false. SQL has a value that stands for "no value is known", so a comparison involving it cannot honestly answer yes or no. SQL adds a third truth value, UNKNOWN, and the logic built on it is called three-valued logic.
Consider commission > 150 for a row where commission is NULL. The real commission might be 200 or 50, and the database cannot tell. The honest answer is UNKNOWN.
The examples in this chapter use the employees table from the sample data. Ada has no manager and no commission. Cy has no commission.
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INTEGER,
commission NUMERIC(8,2)
);
INSERT INTO employees (id, name, manager_id, commission) VALUES
(1, 'Ada', NULL, NULL),
(2, 'Ben', 1, 100.00),
(3, 'Cy', 1, NULL),
(4, 'Di', 2, 300.00);You can see UNKNOWN by selecting a comparison as a column. Most systems display UNKNOWN as NULL, because a boolean NULL is how UNKNOWN is stored.
SELECT name, commission, commission > 150 AS is_high
FROM employees
ORDER BY id;| name | commission | is_high |
|---|---|---|
| Ada | NULL | NULL |
| Ben | 100.00 | false |
| Cy | NULL | NULL |
| Di | 300.00 | true |
SQLite prints 0 and 1 instead of false and true, but the NULL results are the same.
The Truth Tables
Think of UNKNOWN as "could be TRUE or FALSE, and we cannot tell which". The operators then follow from asking whether the answer depends on the missing information.
NOT flips TRUE and FALSE. It leaves UNKNOWN as UNKNOWN, because the opposite of an unknown answer is still unknown.
| a | NOT a |
|---|---|
| TRUE | FALSE |
| FALSE | TRUE |
| UNKNOWN | UNKNOWN |
AND is TRUE only if both sides are TRUE. If either side is FALSE, the result is FALSE even when the other side is UNKNOWN. Otherwise it is UNKNOWN.
| a AND b | TRUE | FALSE | UNKNOWN |
|---|---|---|---|
| **TRUE** | TRUE | FALSE | UNKNOWN |
| **FALSE** | FALSE | FALSE | FALSE |
| **UNKNOWN** | UNKNOWN | FALSE | UNKNOWN |
OR is FALSE only if both sides are FALSE. If either side is TRUE, the result is TRUE even when the other side is UNKNOWN. Otherwise it is UNKNOWN.
| a OR b | TRUE | FALSE | UNKNOWN |
|---|---|---|---|
| **TRUE** | TRUE | TRUE | TRUE |
| **FALSE** | TRUE | FALSE | UNKNOWN |
| **UNKNOWN** | TRUE | UNKNOWN | UNKNOWN |
The tables have one idea behind them. If the unknown value could not change the outcome, the outcome is known. FALSE AND anything is always FALSE, and TRUE OR anything is always TRUE. In every other case the unknown value could decide the result, so the result is UNKNOWN.
You can check the tables directly:
SELECT NULL AND FALSE AS and_false, -- FALSE
NULL AND TRUE AS and_true, -- UNKNOWN, shown as NULL
NULL OR TRUE AS or_true, -- TRUE
NULL OR FALSE AS or_false, -- UNKNOWN, shown as NULL
NOT NULL AS not_null; -- UNKNOWN, shown as NULLThis runs in PostgreSQL, MySQL, and SQLite 3.23 or later. Some systems, such as SQL Server, have no boolean type for a column expression. There you would put these conditions inside a CASE expression or a WHERE clause.
From Three Values to Two: What a Query Keeps
A query must eventually decide whether to keep each row. The rule for WHERE is simple: a row is kept only when the condition is TRUE. Both FALSE and UNKNOWN discard it.
This rule produces the main consequences of three-valued logic. Take the query below.
SELECT name FROM employees WHERE commission <> 100;Ben fails because 100 <> 100 is FALSE. Ada and Cy fail because NULL <> 100 is UNKNOWN. Only Di is returned, so rows with an unknown commission vanish without any error or warning.
The next chapters cover WHERE, HAVING, and CASE in detail. The table below previews how several features collapse the three values into two.
| Feature | Keeps or accepts | Rejects |
|---|---|---|
| `WHERE`, `HAVING`, join `ON` | TRUE | FALSE, UNKNOWN |
| `CASE WHEN` branch | TRUE | FALSE, UNKNOWN |
| `CHECK` constraint | TRUE, UNKNOWN | FALSE |
The CHECK row is the exception, and it surprises people. A check such as CHECK (commission >= 0) accepts a row with a NULL commission, because the result is UNKNOWN and only FALSE is a violation.
Combining Conditions with AND, OR, and NOT
The tables above explain results that look odd at first. Take an OR query:
SELECT name
FROM employees
WHERE commission > 150 OR manager_id = 1;Here is each row evaluated:
| name | commission > 150 | manager_id = 1 | OR result |
|---|---|---|---|
| Ada | UNKNOWN | UNKNOWN | UNKNOWN |
| Ben | FALSE | TRUE | TRUE |
| Cy | UNKNOWN | TRUE | TRUE |
| Di | TRUE | FALSE | TRUE |
The query returns Ben, Cy, and Di. Cy is kept even though his commission is unknown, because UNKNOWN OR TRUE is TRUE. Ada is dropped because both sides are unknown.
Now the same conditions joined with AND:
SELECT name
FROM employees
WHERE commission > 150 AND manager_id = 1;Ada is UNKNOWN AND UNKNOWN, which is UNKNOWN. Ben is FALSE AND TRUE, which is FALSE. Cy is UNKNOWN AND TRUE, which is UNKNOWN. Di is TRUE AND FALSE, which is FALSE. No row evaluates to TRUE, so the result is empty.
NOT Does Not Give You the Complement
With two-valued logic, WHERE x and WHERE NOT x split the table into two groups that together contain every row. With three values they do not.
SELECT name FROM employees WHERE commission > 150; -- Di
SELECT name FROM employees WHERE NOT (commission > 150); -- BenThe two queries return one row each, out of four. Ada and Cy appear in neither result, because NOT UNKNOWN is still UNKNOWN. If you want "everything that is not high", you must say what to do with the unknown rows.
To handle all three outcomes, test each one explicitly. IS TRUE, IS FALSE, and IS UNKNOWN convert a three-valued result into a plain TRUE or FALSE. PostgreSQL and MySQL support all three. SQLite supports IS TRUE, IS FALSE, IS NOT TRUE, and IS NOT FALSE, but not IS UNKNOWN.
-- Rows where the condition is not TRUE: Ada, Ben, Cy.
SELECT name FROM employees WHERE (commission > 150) IS NOT TRUE;
-- Label all three outcomes. Works in every major system.
SELECT name,
CASE
WHEN commission > 150 THEN 'high'
WHEN NOT (commission > 150) THEN 'low'
ELSE 'unknown'
END AS commission_band
FROM employees
ORDER BY id;The CASE returns unknown for Ada and Cy, low for Ben, and high for Di. The ELSE branch catches rows whose conditions were FALSE or UNKNOWN in the earlier branches, so keep the order of the branches in mind.
IN Is a Chain of ORs
IN is shorthand for a series of equality tests joined by OR, so it follows the same truth tables.
SELECT 1 IN (1, NULL) AS a, -- TRUE: 1 = 1 is TRUE, so the OR is TRUE
2 IN (1, NULL) AS b; -- UNKNOWN: FALSE OR UNKNOWNNOT IN negates that whole chain. It becomes a series of <> tests joined by AND, and a single UNKNOWN term can stop the whole thing from ever being TRUE. A later chapter on the NOT IN trap builds on this.
Gotchas
- UNKNOWN is a result, not a stored value. NULL is the stored marker. UNKNOWN is what a condition evaluates to. In a boolean column the two are written the same way, which is why
SELECT NULL AND TRUEdisplays NULL. - Evaluation order is not guaranteed. You cannot rely on
x IS NOT NULL AND 100 / x > 1to avoid dividing by zero or by NULL. The optimizer may evaluate the right side first. UseCASEorNULLIFwhen order matters. - Negating a filter changes more than the rows you expect. Rewriting
WHERE a = 1asWHERE NOT (a = 1)loses the rows whereais NULL. UseIS DISTINCT FROMor an explicitOR a IS NULLto keep them. CHECKandWHEREdisagree. A condition that rejects a row in a query may accept it in a constraint.
Exercises
Exercise
Using the employees table, what does SELECT name FROM employees WHERE NOT (manager_id = 1 OR commission > 250); return? Work out the truth value for each row.
Show answer
It returns no rows. Ada: manager_id = 1 is UNKNOWN and commission > 250 is UNKNOWN, so the OR is UNKNOWN and NOT UNKNOWN is UNKNOWN. Ben: TRUE OR FALSE is TRUE, so NOT gives FALSE. Cy: TRUE OR UNKNOWN is TRUE, so NOT gives FALSE. Di: FALSE OR TRUE is TRUE, so NOT gives FALSE. No row evaluates to TRUE.
Exercise
Write a query that returns the names of employees whose commission is not greater than 150, including the employees whose commission is unknown. Do not use commission IS NULL directly.
Show answer
SELECT name FROM employees WHERE (commission > 150) IS NOT TRUE; This returns Ada, Ben, and Cy. IS NOT TRUE is satisfied by both FALSE (Ben) and UNKNOWN (Ada and Cy). A portable alternative is WHERE NOT COALESCE(commission > 150, FALSE), which also returns Ada, Ben, and Cy.