GREATEST(...)

All functions > COMPARISON > GREATEST(...)

Returns the greatest (maximum) value from a list of arguments.

Syntax

GREATEST(expr, expr, ...)

Notes

  • All arguments must be of the same or compatible types
  • If any argument is NULL, the result is NULL
  • For strings, uses lexicographic (dictionary) ordering
  • For dates/timestamps, returns the latest date/time
  • For ARRAY and ROW, uses the same ordering as < / > (lexicographic / field-by-field)
  • Can accept any number of arguments (not just two)
  • Equivalent to finding the maximum value across multiple columns
  • Opposite of LEAST function

Related Functions

Examples

Numeric values

FeatureQL
SELECT
    f1 := GREATEST(1, 5, 3), -- Numeric maximum
    f2 := GREATEST(1, 1, 1), -- All same values
    f3 := GREATEST(-5, -1, -10), -- Negative numbers
    f4 := GREATEST(NULL::BIGINT, 5, 3) -- NULL input
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
51-1NULL

Other types

FeatureQL
SELECT
    f1 := GREATEST('apple', 'banana', 'cherry'), -- String maximum
    f2 := GREATEST('a', 'A'), -- Case sensitive
    f3 := GREATEST(1.5, 2.3, 1.1) -- Decimal numbers
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
cherrya2.3

Arrays and rows

FeatureQL
SELECT
    f1 := GREATEST(ARRAY(1, 1), ARRAY(1, 2)) -- Lexicographic max of two arrays
;
Result
f1 ARRAY
[1, 2]

Last update at: 2026/05/26 17:22:09