GREATEST
Description
Compares multiple expressions and returns the largest value among them. If any argument is NULL
, returns NULL
.
Syntax
GREATEST(<expr> [, ...])
Parameters
Required Parameter
<expr>
: SupportsTINYINT
,SMALLINT
,INT
,BIGINT
,LARGEINT
,FLOAT
,DOUBLE
,STRING
,DATETIME
, andDECIMAL
types.
Optional Parameters
- Supports multiple arguments.
Return Value
- Returns the largest value among the given expressions.
- If any argument is
NULL
, returnsNULL
.
Usage Notes
- It is recommended to pass arguments of the same type. If argument types differ, the function will attempt to convert them to the same type. For conversion rules, refer to: Type Conversion
- If any argument is NULL, the result will be NULL.
Examples
- Example 1
SELECT GREATEST(-1, 0, 5, 8);
+-----------------------+
| GREATEST(-1, 0, 5, 8) |
+-----------------------+
| 8 |
+-----------------------+ - NULL argument
SELECT GREATEST(-1, 0, 5, NULL);
+--------------------------+
| GREATEST(-1, 0, 5, NULL) |
+--------------------------+
| NULL |
+--------------------------+ - Type conversion
SELECT GREATEST(6, 4.29, 7);
+----------------------+
| GREATEST(6, 4.29, 7) |
+----------------------+
| 7.00 |
+----------------------+The third argument "7" is converted to Decimal type.
- Date type
SELECT GREATEST('2022-02-26 20:02:11', '2020-01-23 20:02:11', '2020-06-22 20:02:11');
+-------------------------------------------------------------------------------+
| GREATEST('2022-02-26 20:02:11', '2020-01-23 20:02:11', '2020-06-22 20:02:11') |
+-------------------------------------------------------------------------------+
| 2022-02-26 20:02:11 |
+-------------------------------------------------------------------------------+