Logical operators
On this page
Logical operators combine boolean expressions and return boolean or NULL.
Use them in expressions, including SELECT and WHERE. A WHERE clause retains only rows whose predicate is TRUE; FALSE and NULL both exclude the row.
List of logical operators
| Operator | Syntax | Description |
|---|---|---|
AND | a AND b | TRUE when both inputs are TRUE. |
OR | a OR b | TRUE when either input is TRUE. |
XOR | a XOR b | TRUE when exactly one input is TRUE. |
NOT | NOT a | Reverses a non-NULL boolean value. |
NULL behavior
ANDreturns FALSE if either input is FALSE. Otherwise, a NULL input returns NULL.ORreturns TRUE if either input is TRUE. Otherwise, a NULL input returns NULL.XORreturns NULL if either input is NULL.NOTreturns NULL for a NULL input.
Examples
Filter customer-impacting failures
Select failed or abandoned checkouts, excluding test events:
VALUES
('evt-17', 'payment_failed', FALSE),
('evt-42', 'payment_failed', TRUE),
('evt-63', 'checkout_abandoned', FALSE),
('evt-84', 'payment_completed', FALSE)
SELECT $0 AS event_id, $1 AS event, $2 AS is_test
WHERE (event = 'payment_failed' OR event = 'checkout_abandoned') AND NOT is_test
SELECT event_id, event+----------+--------------------+
| event_id | event |
+----------+--------------------+
| evt-17 | payment_failed |
| evt-63 | checkout_abandoned |
+----------+--------------------+Validate mutually exclusive outcomes
Use XOR when exactly one outcome flag should be set:
VALUES
('payment-17', TRUE, FALSE),
('payment-42', TRUE, TRUE),
('payment-63', FALSE, FALSE)
SELECT $0 AS payment_id, $1 AS authorized, $2 AS declined
SELECT payment_id, authorized XOR declined AS valid_outcome+------------+---------------+
| payment_id | valid_outcome |
+------------+---------------+
| payment-17 | true |
| payment-42 | false |
| payment-63 | false |
+------------+---------------+