Skip to content

Latest commit

 

History

History
129 lines (103 loc) · 4.76 KB

comparison-operators.rst

File metadata and controls

129 lines (103 loc) · 4.76 KB

psql

Comparison operators

A comparison operator <gloss-operator> tests the relationship between two values and returns a corresponding value of true, false, or NULL.

Table of contents

Basic operators

For simple data types <data-types>, the following basic operators can be used:

Operator Description
< Less than
-------- --------------------------
> Greater than
-------- --------------------------
<= Less than or equal to
-------- --------------------------
>= Greater than or equal to
-------- --------------------------
= Equal
-------- --------------------------
<> Not equal
-------- --------------------------
!= Not equal (same as <>)

When comparing strings, a lexicographical comparison is performed:

cr> select name from locations where name > 'Argabuthon' order by name;
+------------------------------------+
| name                               |
+------------------------------------+
| Arkintoofle Minor                  |
| Bartledan                          |
| Galactic Sector QQ7 Active J Gamma |
| North West Ripple                  |
| Outer Eastern Rim                  |
+------------------------------------+
SELECT 5 rows in set (... sec)

When comparing dates, ISO date formats can be used:

cr> select date, position from locations where date <= '1979-10-12' and
... position < 3 order by position;
+--------------+----------+
| date         | position |
+--------------+----------+
| 308534400000 |        1 |
| 308534400000 |        2 |
+--------------+----------+
SELECT 2 rows in set (... sec)

Tip

Comparison operators are commonly used to filter rows (e.g., in the WHERE <sql-select-where> and HAVING <sql-select-having> clauses of a SELECT <sql-select> statement). However, basic comparison operators can be used as value expressions <sql-operator-invocation> in any context. For example:

cr> SELECT 1 < 10 as my_column;
+-----------+
| my_column |
+-----------+
| TRUE      |
+-----------+
SELECT 1 row in set (... sec)

WHERE clause operators

Within a sql_dql_where_clause, the following operators can also be used:

Operator Description
~ Matches regular expression (case sensitive)
-------------------------- ---------------------------------------------------
~* Matches regular expression (case insensitive)
-------------------------- ---------------------------------------------------
!~ Does not match regular expression (case sensitive)
-------------------------- ---------------------------------------------------

!~*

Does not match regular expression (case insensitive)

-------------------------- ---------------------------------------------------
sql_dql_like Matches a part of the given value
-------------------------- ---------------------------------------------------
sql_dql_not Negates a condition
-------------------------- ---------------------------------------------------
sql_dql_is_null Matches a null value
-------------------------- ---------------------------------------------------
sql_dql_is_not_null Matches a non-null value
-------------------------- ---------------------------------------------------

ip << range

True if IP is within the given IP range (using CIDR notation)

-------------------------- ---------------------------------------------------
x BETWEEN y AND z Shortcut for x >= y AND x <= z
  • sql_array_comparisons
  • sql_subquery_expressions