Comparison Functions
- Capella Operational
- reference
Comparison functions determine the greatest or least value from a set of values.
GREATEST(expr1 , expr2, …)
Description
Returns the greatest value from a list of expressions.
The function compares values based on the sort order of their data types. For example, it compares strings alphabetically and dates chronologically. When comparing values of different data types, the function uses collation rules to determine precedence.
The function requires at least 2 expressions and ignores NULL and MISSING when evaluating values.
Return Value
The function returns:
-
The largest value among the provided expressions.
-
NULLif all expressions areNULLorMISSING.
Examples
SELECT GREATEST(19.50, 15, 21.50, 18) AS greatest_number;
[
{
"greatest_number": 21.5
}
]
SELECT GREATEST("United", "Delta", "American", "Southwest")
AS last_airline_name;
[
{
"last_airline_name": "United"
}
]
When comparing string values, the function uses alphabetical order to determine the greatest value.
SELECT GREATEST(42, "airline", "2025-12-01T00:00:00Z", NULL)
AS greatest_value;
[
{
"greatest_value": "airline"
}
]
The function returns "airline" because strings have a higher precedence than numbers and dates.
LEAST(expr1 , expr2, …)
Description
Returns the smallest value from a list of expressions.
The function compares values based on the sort order of their data types. For example, it compares strings alphabetically and dates chronologically. When comparing values of different data types, the function uses collation rules to determine precedence.
The function requires at least 2 expressions and ignores NULL and MISSING when evaluating values.
Return Value
The function returns:
-
The smallest value among the provided expressions.
-
NULLif all expressions areNULLorMISSING.
Examples
SELECT LEAST(19.50, 15, 21.50, 18) AS lowest_number;
[
{
"lowest_number": 15
}
]
SELECT LEAST("United", "Delta", "American", "Southwest")
AS first_airline_name;
[
{
"first_airline_name": "American"
}
]
When comparing string values, the function uses alphabetical order to determine the least value.
SELECT LEAST(42, "airline", "2025-12-01T00:00:00Z", NULL)
AS least_value;
[
{
"least_value": 42
}
]
The function returns 42 because numbers have a lower precedence than strings and dates.