Conditional

GREATEST

Returns the largest value in a list.

Syntax

GREATEST(<value1>, <value2>, …)

Compares several values in the same row and returns the largest. Its mirror function is LEAST, which returns the smallest. Don't confuse it with MAX: that one searches across many rows, while GREATEST looks at values side by side. Handy for putting a floor under a number: “at least”.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The largest in a list
SELECT GREATEST(3, 7, 5) AS biggest;
biggest
7
A bonus of at least 250
SELECT name, bonus,
GREATEST(bonus, 250) AS bonus_at_least_250
FROM employees;
namebonusbonus_at_least_250
Emma300300
LiamNULL250
Olivia150250
NoahNULL250
Sophia200250

Liam and Noah have no bonus, yet the result isn't NULL: in PostgreSQL, GREATEST skips NULL.

No earlier than August 1
SELECT id, created_at,
GREATEST(created_at, TIMESTAMP '2026-08-01') AS counted_from
FROM orders;
idcreated_atcounted_from
12026-07-03 10:15:002026-08-01 00:00:00
22026-07-28 18:40:002026-08-01 00:00:00
32026-08-09 09:05:002026-08-09 09:05:00
42026-08-21 14:30:002026-08-21 14:30:00
52026-09-02 11:00:002026-09-02 11:00:00

It works with more than numbers: for dates, the “largest” is the latest. July orders are counted from August 1, the rest from their own date.

Watch out

PostgreSQL skips NULL: GREATEST(5, NULL) gives 5, and only when every value is NULL is the result NULL. In MySQL or Oracle, any NULL would give NULL.

The values must have compatible types: GREATEST(5, 'abc') throws invalid input syntax for type integer.

To find the largest value across table rows, use MAX. GREATEST(salary) with a single argument just returns each row's salary.

Related functions