Skip to main content

Comparisons

Comparison Operators

The table below shows the standard comparison operators. Whenever either of the input arguments is NULL, the output of the comparison is NULL.

OperatorDescriptionExampleResult
<less than2 < 3true
>greater than2 > 3false
<=less than or equal to2 <= 3true
>=greater than or equal to4 >= NULLNULL
= or ==equalNULL = NULLNULL
<> or !=not equal2 <> 2false

The table below shows the standard distinction operators. These operators treat NULL values as equal.

OperatorDescriptionExampleResult
IS DISTINCT FROMnot equal, including NULL2 IS DISTINCT FROM NULLtrue
IS NOT DISTINCT FROMequal, including NULLNULL IS NOT DISTINCT FROM NULLtrue

Combination Casting

When performing comparison on different types, Goose performs Combination Casting. These casts were introduced to make interactive querying more convenient and are in line with the casts performed by several programming languages but are often not compatible with PostgreSQL's behavior. For example, the following expressions evaluate and return true in Goose but fail in PostgreSQL.

SELECT 1 = true;
SELECT 1 = '1.1';

It is not possible to enforce stricter type-checking for Goose's comparison operators. If you require stricter type-checking, consider creating a macro with the typeof function or implementing a user-defined function.

BETWEEN and IS [NOT] NULL

Besides the standard comparison operators there are also the BETWEEN and IS (NOT) NULL operators. These behave much like operators, but have special syntax mandated by the SQL standard. They are shown in the table below.

Note that BETWEEN and NOT BETWEEN are only equivalent to the examples below in the cases where both a, x and y are of the same type, as BETWEEN will cast all of its inputs to the same type.

PredicateDescription
a BETWEEN x AND yequivalent to x <= a AND a <= y
a NOT BETWEEN x AND yequivalent to x > a OR a > y
expression IS NULLtrue if expression is NULL, false otherwise
expression ISNULLalias for IS NULL (non-standard)
expression IS NOT NULLfalse if expression is NULL, true otherwise
expression NOTNULLalias for IS NOT NULL (non-standard)

For the expression BETWEEN x AND y, x is used as the lower bound and y is used as the upper bound. Therefore, if x > y, the result will always be false.