SQL Three-Valued Logic and Missing Comparisons
Evaluate conditions with TRUE, FALSE and UNKNOWN and trace which rows survive filtering.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
An ordinary comparison can have an unknown result
If a nullable value is missing, comparing it with \(5\) by equality ordinarily yields UNKNOWN rather than TRUE or FALSE. Negating UNKNOWN still yields UNKNOWN. In a WHERE condition only TRUE passes, so neither equality nor its negation includes those rows. Test missingness explicitly with the language’s null predicate. This is a semantic decision, not an estimate about the likely underlying value.
Operators can use missingness in different ways
An inner join on ordinary equality does not match two missing keys through that predicate. An outer join can produce missing attributes for an unmatched row by construction. A later filter can then remove the very rows the outer join added. Join rewrite laws account for placement of filters. Semijoins and antijoins show how existential tests differ from value comparisons when missing values are present.[1][2]