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.
CREATEORREPLACETABLE logical_test1 (id INT, a INT, b VARCHAR);INSERTINTO logical_test1 (id, a, b)VALUES(1,8,'Up');INSERTINTO logical_test1 (id, a, b)VALUES(2,25,'Down');INSERTINTO logical_test1 (id, a, b)VALUES(3,15,'Down');INSERTINTO logical_test1 (id, a, b)VALUES(4,47,'Up');SELECT*FROM logical_test1;
+----+----+------+| ID | A | B ||----+----+------||1|8| Up ||2|25| Down ||3|15| Down ||4|47| Up |+----+----+------+
Execute queries that use a single logical operator¶
Use a single logical operator in the WHERE clause of various queries:
SELECT*FROM logical_test1
WHERE a >20AND
b ='Down';
+----+----+------+| ID | A | B ||----+----+------||2|25| Down |+----+----+------+
SELECT*FROM logical_test1
WHERE a >20OR
b ='Down';
+----+----+------+| ID | A | B ||----+----+------||2|25| Down ||3|15| Down ||4|47| Up |+----+----+------+
SELECT*FROM logical_test1
WHERE a >20OR
b ='Up';
+----+----+------+| ID | A | B ||----+----+------||1|8| Up ||2|25| Down ||4|47| Up |+----+----+------+
SELECT*FROM logical_test1
WHERENOT a >20;
+----+----+------+| ID | A | B ||----+----+------||1|8| Up ||3|15| Down |+----+----+------+
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:
b equals Down.
OR
a equals 8 AND b equals Up.
SELECT*FROM logical_test1
WHERE b ='Down'OR
a =8AND b ='Up';
+----+----+------+| ID | A | B ||----+----+------||1|8| Up ||2|25| Down ||3|15| Down |+----+----+------+
You can use parentheses in the WHERE clause to change the precedence. For example,
the following query returns the rows that match these conditions:
b equals Down OR a equals 8.
AND
b equals Up.
SELECT*FROM logical_test1
WHERE(b ='Down'OR a =8)AND b ='Up';
+----+---+----+| ID | A | B ||----+---+----||1|8| Up |+----+---+----+
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:
a does NOT equal 15.
AND
b equals Down.
SELECT*FROM logical_test1
WHERENOT a =15AND b ='Down';
+----+----+------+| ID | A | B ||----+----+------||2|25| Down |+----+----+------+
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:
a equals 15.
AND
b equals Down.
SELECT*FROM logical_test1
WHERENOT(a =15AND b ='Down');
+----+----+------+| ID | A | B ||----+----+------||1|8| Up ||2|25| Down ||4|47| Up |+----+----+------+
Use logical operators in queries on Boolean values¶
Create a table and insert data:
CREATEORREPLACETABLE logical_test2 (a BOOLEAN, b BOOLEAN);INSERTINTO logical_test2 VALUES(0,1);SELECT*FROM logical_test2;
+-------+------+| A | B ||-------+------||False|True|+-------+------+
The following query uses the OR operator to return rows where either a or b
is TRUE:
SELECT a, b FROM logical_test2 WHERE a OR b;
+-------+------+| A | B ||-------+------||False|True|+-------+------+
The following query uses the AND operator to return rows where both a and b
are both TRUE:
SELECT a, b FROM logical_test2 WHERE a AND b;
+---+---+| A | B ||---+---|+---+---+
The following query uses the AND operator and the NOT operator to return rows where
b is TRUE and a is FALSE:
SELECT a, b FROM logical_test2 WHERE b ANDNOT a;
+-------+------+| A | B ||-------+------||False|True|+-------+------+
The following query uses the AND operator and the NOT operator to return rows where
a is TRUE and b is FALSE:
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.