TExcelTAD
Home › PostgreSQL › PostgreSQL Operators

PostgreSQL Operators

Operators are the symbols and keywords that compare, combine and calculate values: =, <, +, AND, LIKE and many more. You will use them constantly in WHERE clauses and in computed columns. This lesson gives an overview; later lessons zoom in on the most important ones.

Arithmetic operators

Operator Meaning Example Result
+ - * / add, subtract, multiply, divide 7 / 2 3 (integers!)
% remainder (modulo) 7 % 2 1
^ power 2 ^ 3 8
|/ square root |/ 25 5
@ absolute value @ -4 4
pgsql · try it yourself
SELECT 7 + 2 AS plus, 7 - 2 AS minus, 7 * 2 AS times,
       7 / 2 AS int_div, 7 / 2.0 AS real_div, 7 % 2 AS remainder,
       2 ^ 3 AS power, |/ 25 AS sqrt, @ -4 AS abs;
 plus | minus | times | int_div |      real_div      | remainder | power | sqrt | abs
------+-------+-------+---------+--------------------+-----------+-------+------+-----
    9 |     5 |    14 |       3 | 3.5000000000000000 |         1 |     8 |    5 |   4
(1 row)

Arithmetic works on columns too. What would each book's stock be worth after a 15 % discount?

pgsql · try it yourself
SELECT title, price, stock,
       round(price * 0.85, 2)          AS sale_price,
       round(price * 0.85 * stock, 2)  AS stock_value_on_sale
FROM books
WHERE category_id = 2;
       title        | price | stock | sale_price | stock_value_on_sale
--------------------+-------+-------+------------+---------------------
 Signal from Kepler | 18.00 |     7 |      15.30 |              107.10
 The Last Orbit     | 16.75 |     3 |      14.24 |               42.71
 Clockwork Garden   | 21.00 |    15 |      17.85 |              267.75
(3 rows)

Comparison operators

Operator Meaning
= equal
<> or != not equal
< > <= >= less / greater (or equal)
BETWEEN a AND b inside a range (inclusive)
IN (…) equal to one of a list
LIKE, ILIKE pattern match (case-sensitive / insensitive)
IS NULL, IS NOT NULL test for a missing value
IS DISTINCT FROM "not equal", treating NULL as an ordinary value

A comparison returns a boolean, which psql shows as t or f:

pgsql · try it yourself
SELECT 5 = 5 AS eq, 5 <> 3 AS ne, 'apple' < 'banana' AS text_order,
       DATE '2024-03-01' > DATE '2024-02-29' AS later,
       NULL = NULL AS null_eq,
       NULL IS NOT DISTINCT FROM NULL AS null_same;
 eq | ne | text_order | later | null_eq | null_same
----+----+------------+-------+---------+-----------
 t  | t  | t          | t     |         | t
(1 row)

Notice NULL = NULL: the result is not true but NULL (shown empty), because "unknown equals unknown" is itself unknown. Use IS NULL to test for missing values; the NULL handling lesson explains why.

Logical operators

AND, OR and NOT combine conditions. AND binds tighter than OR, so add parentheses whenever you mix them:

pgsql · try it yourself
-- Science fiction OR mystery books, but only the cheap ones
SELECT title, category_id, price
FROM books
WHERE (category_id = 2 OR category_id = 3) AND price < 15;

-- Without the parentheses the meaning changes: every sci-fi book, plus cheap mysteries
SELECT title, category_id, price
FROM books
WHERE category_id = 2 OR category_id = 3 AND price < 15;
       title        | category_id | price
--------------------+-------------+-------
 Murder at Low Tide |           3 | 11.99
 The Quiet Witness  |           3 | 13.49
 The Map Thief      |           3 | 12.99
(3 rows)

       title        | category_id | price
--------------------+-------------+-------
 Signal from Kepler |           2 | 18.00
 The Last Orbit     |           2 | 16.75
 Clockwork Garden   |           2 | 21.00
 Murder at Low Tide |           3 | 11.99
 The Quiet Witness  |           3 | 13.49
 The Map Thief      |           3 | 12.99
(6 rows)

String and other operators

Operator Meaning Example
|| concatenate text (or arrays) 'Post' || 'gres'
~, ~* regular-expression match (case-sensitive / insensitive) title ~ '^The'
@>, <@ contains / is contained by (arrays, JSONB, ranges) tags @> '{space}'
->, ->> get a JSON field (as JSON / as text) details ->> 'format'
pgsql · try it yourself
SELECT title,
       'Book: ' || title AS label,
       title ~ '^The' AS starts_with_the,
       tags @> '{space}' AS about_space,
       details ->> 'format' AS format
FROM books
WHERE id IN (3, 4, 14);
       title        |          label           | starts_with_the | about_space |  format
--------------------+--------------------------+-----------------+-------------+-----------
 Signal from Kepler | Book: Signal from Kepler | f               | t           | hardcover
 The Last Orbit     | Book: The Last Orbit     | t               | t           | paperback
 Moon Socks         | Book: Moon Socks         | f               | t           | paperback
(3 rows)

Tip: PostgreSQL has hundreds of operators. In psql, \do lists them; in SQL you can query pg_operator. You don't need to memorise them: learn the common ones and look up the rest when needed.

Test yourself

What is the result of SELECT 10 % 3;?

  1. 3
  2. 1
  3. 3.33
  4. 0

What does NULL = NULL return?

  1. true
  2. false
  3. NULL (unknown)
  4. An error

How is a OR b AND c evaluated?

  1. (a OR b) AND c
  2. a OR (b AND c)
  3. Left to right with equal priority
  4. It's a syntax error