GREATEST
All functions > COMPARISON > GREATEST
Returns the greatest (maximum) value from a list of arguments.
Syntax
Diagram(
Sequence(
Terminal("GREATEST"),
Terminal("("),
NonTerminal("expr"),
Terminal(","),
OneOrMore(NonTerminal("expr"), Terminal(",")),
Terminal(")"),
)
)| Parameter | Type | Required | Description |
|---|---|---|---|
expr | T | Yes | Expression to compare |
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
- Can accept any number of arguments (not just two)
- Equivalent to finding the maximum value across multiple columns
- Opposite of LEAST function
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 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 5 | 1 | -1 | NULL |
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 VARCHAR | f2 VARCHAR | f3 VARCHAR |
|---|---|---|
| cherry | a | 2.3 |
On this page