Updated on 2022-03-24 GMT+08:00

Logical Operators

Common logical operators include AND, OR, and NOT. The operation result can be TRUE, FALSE, or NULL (which means unknown). The priorities of the operators are as follows: NOT > AND > OR.

Table 1 lists the calculation rules, where A and B represent logical expressions.

Table 1 Logical operators

Operator

Result Type

Description

A AND B

BOOLEAN

If A and B are TRUE, then TRUE is returned. Otherwise, FALSE is returned. If A or B is NULL, then NULL is returned.

A OR B

BOOLEAN

If A or B is TRUE, then TRUE is returned. Otherwise, FALSE is returned. If A or B is NULL, then NULL is returned. If one is TRUE and the other is NULL, then TRUE is returned.

NOT A

BOOLEAN

If A is FALSE, then TRUE is returned. If A is NULL, then NULL is returned. Otherwise, FALSE is returned.

! A

BOOLEAN

Same as NOT A.

A IN (val1, val2, ...)

BOOLEAN

If A is equal to any value in (val1, val2, ...), then TRUE is returned. Otherwise, FALSE is returned.

A NOT IN (val1, val2, ...)

BOOLEAN

If A is not equal to any value in (val1, val2, ...), then TRUE is returned. Otherwise, FALSE is returned.

EXISTS (subquery)

BOOLEAN

If the result of any subquery contains at least one line, then TRUE is returned. Otherwise, FALSE is returned.

NOT EXISTS (subquery)

BOOLEAN

If the subquery output does not contain any row, TRUE is returned; otherwise, FALSE is returned.