Logical/Boolean Operators

Logical operators return the result of a particular Boolean operation on one or two input expressions.

They can only be used as a predicate (e.g. in the WHERE clause). Input expressions must be predicates.

See also:

BOOLAND , BOOLNOT , BOOLOR , BOOLXOR

In this Topic:

List of Logical/Boolean Operators

Operator

Syntax Example

Description

AND

a AND b

Matches both expressions (a and b).

NOT

NOT a

Does not match the expression.

OR

a OR b

Matches either expression.

The order of precedence of these operators is shown below (from highest to lowest):

  • NOT

  • AND

  • OR

Examples

The first example uses the following table and data:

CREATE TABLE logical (t BOOLEAN, f BOOLEAN, n BOOLEAN);
INSERT INTO logical (t, f, n) VALUES (True, False, NULL);

Logical operators are typically used in WHERE clauses:

SELECT t, f, n FROM logical WHERE t AND NOT f;

Output:

+------+-------+------+
| T    | F     | N    |
|------+-------+------|
| True | False | NULL |
+------+-------+------+

The examples below show the precedence of the logical operators:

The first example shows that the precedence of AND is higher than the precedence of OR. The first expression is evaluated the same way as the second expression, not the same way as the third expression.

SELECT TRUE OR TRUE AND FALSE, TRUE OR (TRUE AND FALSE), (TRUE OR TRUE) AND FALSE;

Output:

+------------------------+--------------------------+--------------------------+
| TRUE OR TRUE AND FALSE | TRUE OR (TRUE AND FALSE) | (TRUE OR TRUE) AND FALSE |
|------------------------+--------------------------+--------------------------|
| True                   | True                     | False                    |
+------------------------+--------------------------+--------------------------+

The next example shows that the precedence of NOT is higher than the precedence of AND. The first expression is evaluated the same way as the second expression, not the same way as the third expression.

SELECT NOT FALSE AND FALSE, (NOT FALSE) AND FALSE, NOT (FALSE AND FALSE);

Output:

+---------------------+-----------------------+-----------------------+
| NOT FALSE AND FALSE | (NOT FALSE) AND FALSE | NOT (FALSE AND FALSE) |
|---------------------+-----------------------+-----------------------|
| False               | False                 | True                  |
+---------------------+-----------------------+-----------------------+

The next example shows that the precedence of NOT is higher than the precedence of OR. The first expression is evaluated the same way as the second expression, not the same way as the third expression.

SELECT NOT FALSE OR TRUE, (NOT FALSE) OR TRUE, NOT (FALSE OR TRUE);

Output:

+-------------------+---------------------+---------------------+
| NOT FALSE OR TRUE | (NOT FALSE) OR TRUE | NOT (FALSE OR TRUE) |
|-------------------+---------------------+---------------------|
| True              | True                | False               |
+-------------------+---------------------+---------------------+

The next few examples show “truth tables” for the logical operators.

Create a new table and data:

CREATE TABLE logical2 (x BOOLEAN);
INSERT INTO logical2 (x) VALUES 
    (False),
    (True),
    (NULL);

This shows the truth table for the OR operator:

SELECT x AS "OR", x OR False AS "FALSE", x OR True AS "TRUE", x OR NULL AS "NULL"
  FROM logical2;

Output:

+-------+-------+------+------+
| OR    | FALSE | TRUE | NULL |
|-------+-------+------+------|
| False | False | True | NULL |
| True  | True  | True | True |
| NULL  | NULL  | True | NULL |
+-------+-------+------+------+

This shows the truth table for the AND operator:

SELECT x AS "AND", x AND False AS "FALSE", x AND True AS "TRUE", x AND NULL AS "NULL"
  FROM logical2;

Output:

+-------+-------+-------+-------+
| AND   | FALSE | TRUE  | NULL  |
|-------+-------+-------+-------|
| False | False | False | False |
| True  | False | True  | NULL  |
| NULL  | False | NULL  | NULL  |
+-------+-------+-------+-------+