9.2. Comparison Functions and Operators
The usual comparison operators are available, as shown in Table 9.1.
Table 9.1. Comparison Operators
||Less than or equal to|
||Greater than or equal to|
<> is the standard SQL notation for “not
!= is an alias, which is converted
<> at a very early stage of parsing.
Hence, it is not possible to implement
<> operators that do different things.
These comparison operators are available for all built-in data types that have a natural ordering, including numeric, string, and date/time types. In addition, arrays, composite types, and ranges can be compared if their component data types are comparable.
It is usually possible to compare values of related data
types as well; for example
bigint will work. Some cases of this sort are implemented
directly by “cross-type” comparison operators, but if no
such operator is available, the parser will coerce the less-general type
to the more-general type and apply the latter's comparison operator.
As shown above, all comparison operators are binary operators that
return values of type
boolean. Thus, expressions like
1 < 2 < 3 are not valid (because there is
< operator to compare a Boolean value with
3). Use the
shown below to perform range tests.
There are also some comparison predicates, as shown in Table 9.2. These behave much like operators, but have special syntax mandated by the SQL standard.
Table 9.2. Comparison Predicates
Between (inclusive of the range endpoints).
Not between (the negation of
Between, after sorting the two endpoint values.
Not between, after sorting the two endpoint values.
Not equal, treating null as a comparable value.
Equal, treating null as a comparable value.
Test whether value is null.
Test whether value is not null.
Test whether value is null (nonstandard syntax).
Test whether value is not null (nonstandard syntax).
Test whether boolean expression yields true.
Test whether boolean expression yields false or unknown.
Test whether boolean expression yields false.
Test whether boolean expression yields true or unknown.
Test whether boolean expression yields unknown.
Test whether boolean expression yields true or false.
is equivalent to
BETWEEN treats the endpoint values as included
in the range.
BETWEEN SYMMETRIC is like
except there is no requirement that the argument to the left of
AND be less than or equal to the argument on the right.
If it is not, those two arguments are automatically swapped, so that
a nonempty range is always implied.
The various variants of
BETWEEN are implemented in
terms of the ordinary comparison operators, and therefore will work for
any data type(s) that can be compared.
The use of
AND in the
syntax creates an ambiguity with the use of
AND as a
logical operator. To resolve this, only a limited set of expression
types are allowed as the second argument of a
clause. If you need to write a more complex sub-expression
BETWEEN, write parentheses around the
Ordinary comparison operators yield null (signifying “unknown”),
not true or false, when either input is null. For example,
7 = NULL yields null, as does
7 <> NULL. When
this behavior is not suitable, use the
IS [ NOT ] DISTINCT FROM predicates:
aIS DISTINCT FROM
aIS NOT DISTINCT FROM
For non-null inputs,
IS DISTINCT FROM is
the same as the
<> operator. However, if both
inputs are null it returns false, and if only one input is
null it returns true. Similarly,
IS NOT DISTINCT
FROM is identical to
= for non-null
inputs, but it returns true when both inputs are null, and false when only
one input is null. Thus, these predicates effectively act as though null
were a normal data value, rather than “unknown”.
expressionIS NOT NULL
or the equivalent, but nonstandard, predicates:
Do not write
expression = NULL
NULL is not “equal to”
NULL. (The null value represents an unknown value,
and it is not known whether two unknown values are equal.)
Some applications might expect that
returns true if
expression = NULL
expression evaluates to
the null value. It is highly recommended that these applications
be modified to comply with the SQL standard. However, if that
cannot be done the transform_null_equals
configuration variable is available. If it is enabled,
PostgreSQL will convert
NULL clauses to
x IS NULL.
expression is row-valued, then
IS NULL is true when the row expression itself is null
or when all the row's fields are null, while
IS NOT NULL is true when the row expression itself is non-null
and all the row's fields are non-null. Because of this behavior,
IS NULL and
IS NOT NULL do not always return
inverse results for row-valued expressions; in particular, a row-valued
expression that contains both null and non-null fields will return false
for both tests. In some cases, it may be preferable to
IS DISTINCT FROM NULL
IS NOT DISTINCT FROM NULL,
which will simply check whether the overall row value is null without any
additional tests on the row fields.
boolean_expressionIS NOT TRUE
boolean_expressionIS NOT FALSE
boolean_expressionIS NOT UNKNOWN
These will always return true or false, never a null value, even when the
operand is null.
A null input is treated as the logical value “unknown”.
IS UNKNOWN and
IS NOT UNKNOWN are
effectively the same as
IS NULL and
IS NOT NULL, respectively, except that the input
expression must be of Boolean type.
Some comparison-related functions are also available, as shown in Table 9.3.
Table 9.3. Comparison Functions