Skip to main content

GREATEST

Functions › Conditional

Returns the largest of its arguments. Unlike MAX, which works across rows, GREATEST compares values within one row.

Syntax​

GREATEST( <expr> [, <expr> ...] )

Arguments​

ArgumentDescription
<expr>One or more values to compare.

Returns​

The largest argument that is not NULL. NULL only when every argument is NULL. The result type joins the argument types: integers and floats give a float; any other mix gives text.

Usage notes​

  • NULL arguments are skipped, as in Postgres: GREATEST(1, NULL, 3) is 3.
  • Numbers compare as numbers, dates and timestamps in time order, and text by character code. Text that reads as a number compares as a number against a number. A value that cannot be compared with the current largest, such as text that is not a number against a number, is skipped.
  • Runs after the rows are read; it is not pushed to the data store.

Examples​

The latest activity on each ticket:

SELECT t.key, GREATEST(t.updated_at, t.last_comment_at, t.closed_at) AS last_activity
FROM Ticket t
keylast_activity
T-12026-10-04 09:12:00.000
T-22026-09-30 16:40:00.000

A floor on a computed value:

SELECT o.key, GREATEST(o.total - o.discount, 0) AS charged
FROM Order o
keycharged
O-100120
O-1010

See also​

LEAST · MAX · COALESCE