Skip to content
WebsiteHosting.com

concept Data & storage

SQL NULL and three-valued logic

An ordinary comparison with NULL yields UNKNOWN rather than FALSE. WHERE selects only TRUE, so a missing value and a known mismatch can both leave a row out for different reasons.

Latest recorded source check · P0241

What it means

In PostgreSQL 18, WHERE keeps a row only when its predicate is TRUE. A row can be left out because the predicate is FALSE or because it is UNKNOWN. The WHERE clause

Consider x = 5. If x is 7, the comparison is FALSE: the values differ. If x is NULL, there is no known value to compare with 5. The result of that ordinary comparison is UNKNOWN, not FALSE. Comparison operators

Try it

The simulation uses three synthetic rows and does not execute SQL.

SQL NULL and three-valued logic

Evaluate a small synthetic row set with comparison, AND/OR/NOT and IS NULL; show true/false/unknown separately and WHERE inclusion.

Simulation

Synthetic inputs only. This is not a measurement of your hardware or service.

Computed under the stated assumptions

Each row keeps TRUE, FALSE and SQL UNKNOWN distinct; WHERE includes only TRUE. Result status remains ok when SQL UNKNOWN was correctly calculated.

SQL NULL and three-valued logic: synthetic scenario inputs

Scenario: Starting example

Each preset replaces all fields. Editing any field preserves the other values exactly, including invalid entries.

WHERE keeps only TRUE

Included row IDs
r1
Controls not used in this predicate
rhs
NULL remains distinct through every truth operation
RowInput valueBase truthAfter combinationAfter NOT selectionIncluded by WHERE
r15truetruetrueYes
r2SQL NULLunknownunknownunknownNo
r37falsefalsefalseNo

Evaluated evidence

Comparison value
5
Base predicate
eq
Logical combination
none
Right-hand truth value
Not established / not applicable
Apply NOT to the combined result
no
Predicate semantics
postgresql-18

Not evaluated in this branch: Right-hand truth value.

Small PostgreSQL 18 scalar predicate subset; no SQL execution, joins, row-value NULL rules, optimizer evaluation order or arbitrary expressions.

About this model

Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.

Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.

No external request, device command, stored-input history or measurement is performed.

Start with Predicate semantics set to postgresql-18, Base predicate set to value = threshold, and Comparison value set to 5. With Logical combination set to None, the Right-hand truth value is unused. With Apply NOT to the combined result set to No, the combined and final truths both match the base truth:

RowInput valueBase truth (x = 5)Included by WHERE?
r15TRUEYes
r2NULLUNKNOWNNo
r37FALSENo

Now follow r2. Its base truth stays UNKNOWN in each of these variations. What changes is the predicate that reaches WHERE. The PostgreSQL truth tables explain the difference:

  • Choose AND with SQL FALSE and leave NOT off. One false operand settles an AND: the combined and final truths are FALSE, so r2 stays out.
  • Choose OR with SQL TRUE and leave NOT off. One true operand settles an OR: the combined and final truths are TRUE, so r2 gets in even though its value is still NULL.
  • Switch the combination back to None and turn NOT on. NOT UNKNOWN is still UNKNOWN, so r2 stays out. By contrast, r3 now gets in: its base truth was FALSE, which NOT turns into TRUE.

Comparison value takes whole-number decimal text from -100 through 100. Enter 5, not 5.0 or a blank field; the latter inputs are invalid, not SQL NULL. Reset restores the starting settings and clears errors, history, and the prior comparison.

In practice

Choose the predicate that matches your question. If you want the rows with a missing value, use x IS NULL, not x = NULL. IS NULL tests the scalar value directly rather than comparing two values for equality. Comparison predicates The Test IS NULL instead of equality preset does not use the threshold in its predicate: its base truths are FALSE, TRUE, FALSE, and it selects r2 alone. The comparison field must still contain valid whole-number text for the simulation to accept the input.

A row’s SQL UNKNOWN is a computed truth value; the default simulation has status ok even though r2’s equality comparison is UNKNOWN. The Inspect the evidence boundary preset instead leaves the predicate rules unspecified. Its status unknown means no row result was established. The simulation covers only these scalar predicates, not joins, row-value NULL rules, or optimizer evaluation order.

When a row is missing from a filtered result, work out the predicate’s truth before interpreting the omission. Exclusion tells you the final predicate was not TRUE; it does not tell you it was FALSE.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Why does comparing NULL not produce true?

An ordinary comparison cannot establish equality from a missing value. In the scoped PostgreSQL scalar rules, it returns SQL unknown rather than true.

Can an unknown predicate become true through OR?

Yes. SQL unknown OR true is true, so WHERE can retain that row. Unknown AND false is false.

How does IS NULL differ from equality?

IS NULL directly tests whether the scalar value is null. It does not perform an ordinary equality comparison with a missing operand.

Is SQL unknown an error in the simulator?

No. A correctly calculated SQL unknown is a valid truth value. The separate simulator status unknown means a required rule or evidence was not supplied.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL 18: Logical Operators

    9.1 AND, OR, NOT truth tables. SQL logical evaluation distinguishes true, false and null/unknown; the documentation gives the applicable three-valued truth tables.

    Checked .

  2. PostgreSQL 18: Comparison Functions and Operators

    9.2 comparison predicates and NULL. Ordinary comparisons with NULL produce unknown; IS NULL directly tests for null rather than using ordinary equality.

    Checked .

  3. PostgreSQL 18: Table Expressions

    7.2.2 The WHERE Clause. WHERE keeps rows whose predicate is true; false and null results do not select a row.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.