Logical Operators
Logical operators combine conditions and enable complex filtering expressions. They allow you to express compound conditions by combining simpler conditions using boolean logic.
Available Operators
AND
The AND
operator combines two conditions and returns true only when both conditions are true.
Syntax:
<condition1> AND <condition2>
Examples:
category = 'Electronics' AND price < 500
status = 'active' AND createdDate > '2023-01-01'
country = 'USA' AND state = 'California' AND city = 'San Francisco'
Truth table:
Condition 1 | Condition 2 | Result |
---|---|---|
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
OR
The OR
operator combines two conditions and returns true when either condition is true or both are true.
Syntax:
<condition1> OR <condition2>
Examples:
category = 'Electronics' OR category = 'Computers'
price < 100 OR onSale = true
country = 'USA' OR country = 'Canada' OR country = 'Mexico'
Truth table:
Condition 1 | Condition 2 | Result |
---|---|---|
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
Operator Precedence
In the View query language, logical operators have the following precedence (from highest to lowest):
-
NOT
-
AND
-
OR
This means that:
-
NOT a AND b
is evaluated as(NOT a) AND b
-
a AND b OR c
is evaluated as(a AND b) OR c
-
a OR b AND c
is evaluated asa OR (b AND c)
Use parentheses to override the default precedence:
(category = 'Electronics' OR category = 'Computers') AND price < 1000
Complex Expressions
Logical operators can be combined to create complex conditions:
(category = 'Electronics' OR category = 'Computers')
AND (price < 1000 OR onSale = true)
AND NOT discontinued = true
Alternative Expressions
Many complex logical expressions can be simplified using other operators:
Complex Expression | Simpler Alternative |
---|---|
|
|
|
|
|
|
|
|
|
|
De Morgan’s Laws
These logical equivalences can help simplify negated expressions:
-
NOT (a AND b)
is equivalent toNOT a OR NOT b
-
NOT (a OR b)
is equivalent toNOT a AND NOT b
Notes
-
Use parentheses to make complex expressions more readable and to ensure the correct evaluation order
-
When combining many OR conditions with the same column, consider using the IN operator instead
-
For checking if a value is in an array column, use the = ANY operator
-
Logical operators work with any expressions that evaluate to boolean values
Related Features
-
Operators Overview - All available operators
-
Comparison Operators - =, !=, >, <, etc.
-
IN Operator - Shorthand for multiple OR conditions with equality
-
= ANY Operator - Tests if a value is in an array
-
WHERE Clause - Using logical operators for filtering