# Logical operators

[Read this page in HTML](https://www.scopedb.io/docs/reference/commands/query-operators/logical)

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

* `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:

```scopeql
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
```

```text
+----------+--------------------+
| 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:

```scopeql
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
```

```text
+------------+---------------+
| payment_id | valid_outcome |
+------------+---------------+
| payment-17 | true          |
| payment-42 | false         |
| payment-63 | false         |
+------------+---------------+
```
