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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

GREATEST(value, ...)  |  LEAST(value, ...)

Parameters

ParameterDescription
value Values of compatible types.

Return value

The largest (or smallest) non-NULL value.

Examples

Largest and smallest
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.

Never below zero
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

Was this page helpful?