GREATEST(...)

All functions > COMPARISON > GREATEST(...)

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

Signatures

Returns: Greatest of the arguments, or NULL if any argument is NULL

GREATEST_FN(exprs: T, ...) → T
sql
ParameterTypeRequiredDescription
exprsT, ...YesVariadic arguments in SQL (at least one; all mutually compatible)

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 < / > (see Related operators for variadic GREATEST(...) examples)
  • Can accept any number of arguments (not just two)
  • Equivalent to finding the maximum value across multiple columns
  • Opposite of LEAST function

Related operators

Examples

GREATEST_FN — single-array encoding

FeatureQL
SELECT
    f1 := GREATEST_FN(ARRAY[1, 5, 3]), -- Same semantics as variadic `GREATEST`
    f2 := GREATEST_FN(ARRAY['a', 'z', 'm']) -- Lexicographic max via array encoding
;
Result
f1 BIGINTf2 VARCHAR
5z

GREATEST_FN — NULL propagation

FeatureQL
SELECT
    f1 := GREATEST_FN(ARRAY[NULL::BIGINT, 3]), -- Any NULL in the list → NULL result
    f2 := GREATEST_FN(ARRAY[1, NULL::BIGINT, 3]) -- NULL anywhere → NULL result
;
Result
f1 BIGINTf2 BIGINT
NULLNULL

GREATEST_FN — arrays of arrays

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

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