Logical operators¶
Logical operators return the result of a particular Boolean operation on one or two input expressions. Logical operators are also referred to as Boolean operators.
Logical operators can only be used as a predicate (for example, in the WHERE clause). Input expressions must be predicates.
List of logical operators¶
Operator |
Syntax example |
Description |
|---|---|---|
|
|
Matches both expressions ( |
|
|
Doesn’t match the expression. |
|
|
Matches either expression. |
The order of precedence of these operators is shown below (from highest to lowest):
NOT
AND
OR
Examples¶
The following examples use logical operators:
Use logical operators in queries on table data¶
Create a table and insert data:
Execute queries that use a single logical operator¶
Use a single logical operator in the WHERE clause of various queries:
Show the precedence of logical operators¶
The following examples show the precedence of the logical operators.
The first example shows that the precedence of AND is higher than the precedence of OR. The query returns the rows that match these conditions:
bequalsDown.
OR
aequals8ANDbequalsUp.
You can use parentheses in the WHERE clause to change the precedence. For example, the following query returns the rows that match these conditions:
bequalsDownORaequals8.
AND
bequalsUp.
The next example shows that the precedence of NOT is higher than the precedence of AND. For example, the following query returns the rows that match these conditions:
adoes NOT equal15.
AND
bequalsDown.
You can use parentheses in the WHERE clause to change the precedence. For example, the following query returns the rows that do NOT match both of these conditions:
aequals15.
AND
bequalsDown.
Use logical operators in queries on Boolean values¶
Create a table and insert data:
The following query uses the OR operator to return rows where either a or b
is TRUE:
The following query uses the AND operator to return rows where both a and b
are both TRUE:
The following query uses the AND operator and the NOT operator to return rows where
b is TRUE and a is FALSE:
The following query uses the AND operator and the NOT operator to return rows where
a is TRUE and b is FALSE:
Show “truth tables” for the logical operators¶
The next few examples show “truth tables” for the logical operators on a Boolean column. For more information about the behavior of Boolean values in Snowflake, see Ternary logic.
Create a new table and data:
This shows the truth table for the OR operator:
This shows the truth table for the AND operator: