Logical Functions

Logical Operators

SQL uses a three-valued logic system with true, false, and null which represents “missing”.

The basic logical operators are available: AND, OR, NOT. The operators AND and OR are commutative, that is, you can switch the left and right operand without affecting the result. Observe the following truth tables:

aba AND ba OR b
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse
truenullnulltrue
nulltruenulltrue
falsenullfalsenull
nullfalsefalsenull
nullnullnullnull
aNOT aa IS NULLa IS NOT NULL
truefalsefalsetrue
falsetruefalsetrue
nullnulltruefalse

Logical Functions

Beware, the logical operators are turned into proper identifiers by double quoting to avoid clashing with the corresponding operator.

FunctionReturn typeDescriptionExampleResult
"all"(a boolean, b boolean, c boolean)booleanif c is null then true else if a is false then false else if (b is true or c is true) then null else true"all"(true, false, true)null
"and"(a boolean, b boolean)booleana AND b"and"(true, false)false
"any"(a boolean, b boolean, c boolean)booleanif c is null then false else if a is true then true else if (b is true or c is true) then null else false"any"(true, false, true)true
"not"(a boolean)booleanNOT a"not"(true)false
"or"(a boolean, b boolean)booleana OR b"or"(true, false)true
"xor"(a boolean, b boolean)booleana OR b, but NOT, a AND b"xor"(true, true)false