Ternary logic¶
As specified in the SQL standard, ternary logic, or three-valued logic (3VL), is a logic system with three truth values: TRUE, FALSE, and UNKNOWN. In Snowflake, UNKNOWN is represented by NULL. Ternary logic applies to the evaluation of Boolean expressions, as well as predicates, and affects the results of logical operations such as AND, OR, and NOT:
When used in expressions (for example, in a SELECT list), UNKNOWN results are returned as NULL values.
When used as a predicate (for example, in a WHERE clause), UNKNOWN results evaluate to FALSE.
Truth tables¶
This section describes the truth tables for the comparison and logical operators.
Comparison operators¶
If any operand for a comparison operator is NULL, the result is NULL. The comparison operators and functions are:
Logical operators¶
Given a BOOLEAN column C
:
If |
|
|
|
---|---|---|---|
TRUE |
NULL |
TRUE |
FALSE |
FALSE |
FALSE |
NULL |
TRUE |
NULL |
NULL |
NULL |
NULL |
In addition:
If |
|
|
|
---|---|---|---|
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
NULL |
NULL |
NULL |
NULL |
NULL |
Usage notes for conditional expressions¶
This section describes behavior specific to conditional expressions.
IFF behavior¶
IFF returns the following results for ternary logic. Given a BOOLEAN column C
:
If |
|
---|---|
TRUE |
|
FALSE |
|
NULL |
|
[ NOT ] IN behavior¶
[ NOT ] IN returns the following results for ternary logic. Given 3 numeric columns c1
, c2
, and c3
:
c1 IN (c2, c3, ...)
is syntactically equivalent to(c1 = c2 OR c1 = c3 OR ...)
.As a result, when the value of
c1
is NULL, the expressionc1 IN (c2, c3, NULL)
always evaluates to NULL.c1 NOT IN (c2, c3, ... )
is syntactically equivalent to(c1 <> c2 AND c1 <> c3 AND ...)
.Therefore, even if
c1 NOT IN (c2, c3)
is TRUE,c1 NOT IN (c2, c3, NULL)
evaluates to NULL.