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
  1. An ordinary comparison can have an unknown result
  2. Operators can use missingness in different ways

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]

References

  1. PostgreSQL Global Development Group, PostgreSQL Documentation ↩
  2. CMU 15-445/645, Introduction to Database Systems: Lectures ↩