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 NULLa = NULL
truefalsefalsetruenull
falsetruefalsetruenull
nullnulltruefalsenull

Note that testing if a column value is NULL should be done with a IS NULL and not with a = NULL as the equals (=) comparison always results to null!

Logical Functions

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

FunctionReturn typeDescriptionExampleResult
"and"(a boolean, b boolean)booleana AND b"and"(true, false)false
"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