Logical operators

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

OperatorSyntaxDescription
ANDa AND bTRUE when both inputs are TRUE.
ORa OR bTRUE when either input is TRUE.
XORa XOR bTRUE when exactly one input is TRUE.
NOTNOT aReverses a non-NULL boolean value.

NULL behavior

  • AND returns FALSE if either input is FALSE. Otherwise, a NULL input returns NULL.
  • OR returns TRUE if either input is TRUE. Otherwise, a NULL input returns NULL.
  • XOR returns NULL if either input is NULL.
  • NOT returns 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         |
+------------+---------------+