---

slug: /logical-operators
---

# Logical operators

seekdb supports common logical operators.

## Overview of logical operators

The following table describes the logical operators supported in the current version of seekdb.

|    Operator     | Operands |  Description  |
|------------|-----|------|
| `NOT`/`!`  | Unary  | Logical NOT  |
| `AND`/`&&` | Binary  | Logical AND  |
| `OR`/`||`  | Binary  | Logical OR  |
| `XOR`      | Multiple  | Logical XOR  |

## Description

In SQL, the result of any logical operator is `TRUE`, `FALSE`, or `NULL (UNKNOWN)`. In seekdb, they are implemented as 1 (`TRUE`), 0 (`FALSE`), and `NULL`, and any non-zero, non-`NULL` value is evaluated as `TRUE`.

## Limitations

The `ANY` operator is supported only for array data types. For more information about the usage and examples, see [Array decision functions](https://en.oceanbase.com/docs/common-oceanbase-database-10000000001976559).

### NOT/!

If the operand is 0, the result is 1. If the operand is non-zero, the result is 0, and `NOT NULL` returns `NULL`.

Here is an example:

```sql
SELECT NOT NULL;
+----------+
| NOT NULL |
+----------+
|     NULL |
+----------+
1 row in set (0.001 sec)

SELECT ! (1+1);
+---------+
| ! (1+1) |
+---------+
|       0 |
+---------+
1 row in set (0.001 sec)
```

### AND/\&\&

If all operands are non-zero and not `NULL`, the result is 1. If one or more operands are 0, the result is 0. Otherwise, `NULL` is returned.

Here is an example:

```sql
SELECT 1 AND NULL;
+------------+
| 1 AND NULL |
+------------+
|       NULL |
+------------+
1 row in set (0.001 sec)

SELECT 0 AND NULL;
+------------+
| 0 AND NULL |
+------------+
|          0 |
+------------+
1 row in set (0.000 sec)
```

### OR/\|\|

If both operands are not `NULL`, the result is 1 if any operand is non-zero, otherwise 0. For `NULL` operands, if the other operand is non-zero, the result is 1; otherwise, `NULL` is returned. If both operands are `NULL`, the result is `NULL`.

Here is an example:

```sql
SELECT 0 OR NULL;
+-----------+
| 0 OR NULL |
+-----------+
|      NULL |
+-----------+
1 row in set (0.001 sec)

SELECT 1 OR NULL;
+-----------+
| 1 OR NULL |
+-----------+
|         1 |
+-----------+
1 row in set (0.000 sec)
```

### XOR

If any operand is `NULL`, `NULL` is returned. For non-`NULL` operands, if an odd number of operands are non-zero, the result is 1; otherwise, 0 is returned.

Here is an example:

```sql
SELECT 1 XOR NULL;
+------------+
| 1 XOR NULL |
+------------+
|       NULL |
+------------+
1 row in set (0.001 sec)

SELECT 1 XOR 1 XOR 1;
+---------------+
| 1 XOR 1 XOR 1 |
+---------------+
|             1 |
+---------------+
1 row in set (0.001 sec)
```

  :::info

  `a XOR b` is mathematically equivalent to `(a AND (NOT b)) OR ((NOT a) and b)`.

  :::