SQL
5 min read
BEGINNER
Three-valued logic
Quick AnswerSQL conditions may be TRUE, FALSE or UNKNOWN. A comparison involving NULL usually produces UNKNOWN, represented by NULL in a result. WHERE keeps only rows whose condition is TRUE.
Core idea
SQL conditions may be TRUE, FALSE or UNKNOWN. A comparison involving NULL usually produces UNKNOWN, represented by NULL in a result. WHERE keeps only rows whose condition is TRUE.
Minimal example
sql
SELECT TRUE AND NULL, FALSE AND NULL, TRUE OR NULL, NOT NULL;Expected SQLite rows:
text
NULL | 0 | 1 | NULLHow it works
TRUE AND UNKNOWN is UNKNOWN, FALSE AND UNKNOWN is FALSE, TRUE OR UNKNOWN is TRUE, and NOT UNKNOWN remains UNKNOWN. SQLite displays true and false as 1 and 0; PostgreSQL uses Boolean values.
Practical example
sql
SELECT id FROM sort_scores WHERE NOT (score = 10) ORDER BY id;Expected SQLite rows:
text
3Common mistake and practice
Do not assume NOT makes an unknown comparison true. Predict which rows satisfy NOT (score = 10), then run the query.