The usual comparison operators are available, shown in Table 9.1, “Comparison Operators”.
Table 9.1. Comparison Operators
| Operator | Description |
|---|---|
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
= |
equal |
<> or != |
not equal |
The != operator is converted to
<> in the parser stage. It is not
possible to implement != and
<> operators that do different things.
Comparison operators are available for all data types where this
makes sense. All comparison operators are binary operators that
return values of type boolean; expressions like
1 < 2 < 3 are not valid (because there is
no < operator to compare a Boolean value with
3).
In addition to the comparison operators, the special
BETWEEN construct is available.
aBETWEENxANDy
is equivalent to
a>=xANDa<=y
Similarly,
aNOT BETWEENxANDy
is equivalent to
a<xORa>y
There is no difference between the two respective forms apart from
the CPU cycles required to rewrite the first one
into the second one internally.
BETWEEN SYMMETRIC is the same as BETWEEN
except there is no requirement that the argument to the left of AND be less than
or equal to the argument on the right; the proper range is automatically determined.
To check whether a value is or is not null, use the constructs
expressionIS NULLexpressionIS NOT NULL
or the equivalent, but nonstandard, constructs
expressionISNULLexpressionNOTNULL
Do not write
because expression = NULLNULL is not “equal to”
NULL. (The null value represents an unknown value,
and it is not known whether two unknown values are equal.) This
behavior conforms to the SQL standard.
Some applications may expect that
returns true if expression = NULLexpression 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 x =
NULL clauses to x IS NULL. This was
the default behavior in PostgreSQL
releases 6.5 through 7.1.
The ordinary comparison operators yield null (signifying “unknown”)
when either input is null. Another way to do comparisons is with the
IS DISTINCT FROM construct:
expressionIS DISTINCT FROMexpression
For non-null inputs this is the same as the <> operator.
However, when both inputs are null it will return false, and when just
one input is null it will return true. Thus it effectively acts as though
null were a normal data value, rather than “unknown”.
Boolean values can also be tested using the constructs
expressionIS TRUEexpressionIS NOT TRUEexpressionIS FALSEexpressionIS NOT FALSEexpressionIS UNKNOWNexpressionIS 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”.
Notice that 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.