PostgreSQL 18 reference · Conditional and Type Expressions
PostgreSQL GREATEST() Function
Return the largest of a list of values, ignoring NULLs. least() returns the smallest.
PostgreSQL 18
Syntax
GREATEST(value, ...) | LEAST(value, ...)
Parameters
| Parameter | Description |
|---|---|
value |
Values of compatible types. |
Return value
The largest (or smallest) non-NULL value.
Examples
SELECT greatest(3, 9, 4) AS biggest, least(3, 9, 4) AS smallest, greatest(1, NULL) AS ignores_null;
Output
biggest | smallest | ignores_null
---------+----------+--------------
9 | 3 | 1
(1 row)Run this on your computer — see “Get Started” for setup.
SELECT name, price, greatest(price - 10, 0) AS after_10_off
FROM products
WHERE product_id IN (8, 11);
Output
name | price | after_10_off -------------+-------+-------------- Coffee Mug | 12.00 | 2.00 Rubber Duck | 5.50 | 0 (2 rows)
Run this on your computer — see “Get Started” for setup.
In other languages
Related
COALESCE()· Return the first argument that isn't NULL. The standard way to replace NULL with a default.MAX()· Return the largest value in a group. Works with numbers, text, dates and, since PostgreSQL 18, arrays.- SQL Aggregate Functions · tutorial