2 9.2. Comparison Functions and Operators #
4 The usual comparison operators are available, as shown in Table 9.1.
6 Table 9.1. Comparison Operators
8 datatype < datatype → boolean Less than
9 datatype > datatype → boolean Greater than
10 datatype <= datatype → boolean Less than or equal to
11 datatype >= datatype → boolean Greater than or equal to
12 datatype = datatype → boolean Equal
13 datatype <> datatype → boolean Not equal
14 datatype != datatype → boolean Not equal
18 <> is the standard SQL notation for “not equal”. != is an alias, which
19 is converted to <> at a very early stage of parsing. Hence, it is not
20 possible to implement != and <> operators that do different things.
22 These comparison operators are available for all built-in data types
23 that have a natural ordering, including numeric, string, and date/time
24 types. In addition, arrays, composite types, and ranges can be compared
25 if their component data types are comparable.
27 It is usually possible to compare values of related data types as well;
28 for example integer > bigint will work. Some cases of this sort are
29 implemented directly by “cross-type” comparison operators, but if no
30 such operator is available, the parser will coerce the less-general
31 type to the more-general type and apply the latter's comparison
34 As shown above, all comparison operators are binary operators that
35 return values of type boolean. Thus, expressions like 1 < 2 < 3 are not
36 valid (because there is no < operator to compare a Boolean value with
37 3). Use the BETWEEN predicates shown below to perform range tests.
39 There are also some comparison predicates, as shown in Table 9.2. These
40 behave much like operators, but have special syntax mandated by the SQL
43 Table 9.2. Comparison Predicates
51 datatype BETWEEN datatype AND datatype → boolean
53 Between (inclusive of the range endpoints).
59 datatype NOT BETWEEN datatype AND datatype → boolean
61 Not between (the negation of BETWEEN).
63 2 NOT BETWEEN 1 AND 3 → f
65 datatype BETWEEN SYMMETRIC datatype AND datatype → boolean
67 Between, after sorting the two endpoint values.
69 2 BETWEEN SYMMETRIC 3 AND 1 → t
71 datatype NOT BETWEEN SYMMETRIC datatype AND datatype → boolean
73 Not between, after sorting the two endpoint values.
75 2 NOT BETWEEN SYMMETRIC 3 AND 1 → f
77 datatype IS DISTINCT FROM datatype → boolean
79 Not equal, treating null as a comparable value.
81 1 IS DISTINCT FROM NULL → t (rather than NULL)
83 NULL IS DISTINCT FROM NULL → f (rather than NULL)
85 datatype IS NOT DISTINCT FROM datatype → boolean
87 Equal, treating null as a comparable value.
89 1 IS NOT DISTINCT FROM NULL → f (rather than NULL)
91 NULL IS NOT DISTINCT FROM NULL → t (rather than NULL)
93 datatype IS NULL → boolean
95 Test whether value is null.
99 datatype IS NOT NULL → boolean
101 Test whether value is not null.
103 'null' IS NOT NULL → t
105 datatype ISNULL → boolean
107 Test whether value is null (nonstandard syntax).
109 datatype NOTNULL → boolean
111 Test whether value is not null (nonstandard syntax).
113 boolean IS TRUE → boolean
115 Test whether boolean expression yields true.
119 NULL::boolean IS TRUE → f (rather than NULL)
121 boolean IS NOT TRUE → boolean
123 Test whether boolean expression yields false or unknown.
127 NULL::boolean IS NOT TRUE → t (rather than NULL)
129 boolean IS FALSE → boolean
131 Test whether boolean expression yields false.
135 NULL::boolean IS FALSE → f (rather than NULL)
137 boolean IS NOT FALSE → boolean
139 Test whether boolean expression yields true or unknown.
141 true IS NOT FALSE → t
143 NULL::boolean IS NOT FALSE → t (rather than NULL)
145 boolean IS UNKNOWN → boolean
147 Test whether boolean expression yields unknown.
151 NULL::boolean IS UNKNOWN → t (rather than NULL)
153 boolean IS NOT UNKNOWN → boolean
155 Test whether boolean expression yields true or false.
157 true IS NOT UNKNOWN → t
159 NULL::boolean IS NOT UNKNOWN → f (rather than NULL)
161 The BETWEEN predicate simplifies range tests:
167 Notice that BETWEEN treats the endpoint values as included in the
168 range. BETWEEN SYMMETRIC is like BETWEEN except there is no requirement
169 that the argument to the left of AND be less than or equal to the
170 argument on the right. If it is not, those two arguments are
171 automatically swapped, so that a nonempty range is always implied.
173 The various variants of BETWEEN are implemented in terms of the
174 ordinary comparison operators, and therefore will work for any data
175 type(s) that can be compared.
179 The use of AND in the BETWEEN syntax creates an ambiguity with the use
180 of AND as a logical operator. To resolve this, only a limited set of
181 expression types are allowed as the second argument of a BETWEEN
182 clause. If you need to write a more complex sub-expression in BETWEEN,
183 write parentheses around the sub-expression.
185 Ordinary comparison operators yield null (signifying “unknown”), not
186 true or false, when either input is null. For example, 7 = NULL yields
187 null, as does 7 <> NULL. When this behavior is not suitable, use the IS
188 [ NOT ] DISTINCT FROM predicates:
190 a IS NOT DISTINCT FROM b
192 For non-null inputs, IS DISTINCT FROM is the same as the <> operator.
193 However, if both inputs are null it returns false, and if only one
194 input is null it returns true. Similarly, IS NOT DISTINCT FROM is
195 identical to = for non-null inputs, but it returns true when both
196 inputs are null, and false when only one input is null. Thus, these
197 predicates effectively act as though null were a normal data value,
198 rather than “unknown”.
200 To check whether a value is or is not null, use the predicates:
202 expression IS NOT NULL
204 or the equivalent, but nonstandard, predicates:
208 Do not write expression = NULL because NULL is not “equal to” NULL.
209 (The null value represents an unknown value, and it is not known
210 whether two unknown values are equal.)
214 Some applications might expect that expression = NULL returns true if
215 expression evaluates to the null value. It is highly recommended that
216 these applications be modified to comply with the SQL standard.
217 However, if that cannot be done the transform_null_equals configuration
218 variable is available. If it is enabled, PostgreSQL will convert x =
219 NULL clauses to x IS NULL.
221 If the expression is row-valued, then IS NULL is true when the row
222 expression itself is null or when all the row's fields are null, while
223 IS NOT NULL is true when the row expression itself is non-null and all
224 the row's fields are non-null. Because of this behavior, IS NULL and IS
225 NOT NULL do not always return inverse results for row-valued
226 expressions; in particular, a row-valued expression that contains both
227 null and non-null fields will return false for both tests. For example:
228 SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same');
230 SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows
232 SELECT ROW(table.*) IS NOT NULL FROM table; -- detect all-non-null rows
234 SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in
237 In some cases, it may be preferable to write row IS DISTINCT FROM NULL
238 or row IS NOT DISTINCT FROM NULL, which will simply check whether the
239 overall row value is null without any additional tests on the row
242 Boolean values can also be tested using the predicates
243 boolean_expression IS TRUE
244 boolean_expression IS NOT TRUE
245 boolean_expression IS FALSE
246 boolean_expression IS NOT FALSE
247 boolean_expression IS UNKNOWN
248 boolean_expression IS NOT UNKNOWN
250 These will always return true or false, never a null value, even when
251 the operand is null. A null input is treated as the logical value
252 “unknown”. Notice that IS UNKNOWN and IS NOT UNKNOWN are effectively
253 the same as IS NULL and IS NOT NULL, respectively, except that the
254 input expression must be of Boolean type.
256 Some comparison-related functions are also available, as shown in
259 Table 9.3. Comparison Functions
267 num_nonnulls ( VARIADIC "any" ) → integer
269 Returns the number of non-null arguments.
271 num_nonnulls(1, NULL, 2) → 2
273 num_nulls ( VARIADIC "any" ) → integer
275 Returns the number of null arguments.
277 num_nulls(1, NULL, 2) → 1