<>is the standard SQL notation for “not equal”.
!=is an alias, which is converted to
<>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
bigintwill 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 < 3are not valid (because there is no
<operator to compare a Boolean value with
3). Use the
BETWEENpredicates shown below to perform range tests.
BETWEENpredicate simplifies range tests:
a BETWEEN x AND y
is equivalent to
a >= x AND a <= y
BETWEENtreats the endpoint values as included in the range.
BETWEEN SYMMETRICis like
BETWEENexcept there is no requirement that the argument to the left of
ANDbe 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
BETWEENare implemented in terms of the ordinary comparison operators, and therefore will work for any data type(s) that can be compared.
The use of
BETWEENsyntax creates an ambiguity with the use of
ANDas a logical operator. To resolve this, only a limited set of expression types are allowed as the second argument of a
BETWEENclause. If you need to write a more complex sub-expression in
BETWEEN, write parentheses around the sub-expression.
Ordinary comparison operators yield null (signifying “unknown”), not true or false, when either input is null. For example,
7 = NULLyields null, as does
7 <> NULL. When this behavior is not suitable, use the
IS [ NOT ] DISTINCT FROMpredicates:
a IS DISTINCT FROM b
a IS NOT DISTINCT FROM b
For non-null inputs,
IS DISTINCT FROMis 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 FROMis 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”.
To check whether a value is or is not null, use the predicates:
expression IS NULL
expression IS NOT NULL
or the equivalent, but nonstandard, predicates:
Do not write
NULLis 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
= NULLreturns true if
expressionevaluates 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 = NULLclauses to
x IS NULL.
expressionis row-valued, then
IS NULLis true when the row expression itself is null or when all the row's fields are null, while
IS NOT NULLis true when the row expression itself is non-null and all the row's fields are non-null. Because of this behavior,
IS NOT NULLdo 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 write
IS DISTINCT FROM NULLor
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 values can also be tested using the predicates
boolean_expression IS TRUE
boolean_expression IS NOT TRUE
boolean_expression IS FALSE
boolean_expression IS NOT FALSE
boolean_expression IS UNKNOWN
boolean_expression IS 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 NOT UNKNOWNare effectively the same as
IS NOT NULL, respectively, except that the input expression must be of Boolean type.