GREATEST

greatest(expr1[, expr2, ...])

Description

The GREATEST function is used to find and return the maximum value from the given list of parameters. If there are null values among the parameters, the null values will be ignored.

Parameter Description

  • expr: Data of comparable types, including but not limited to the following types:
    • Numeric types: float, double, decimal, tinyint, smallint, int, bigint
    • String types: char, varchar, string
    • Binary type
    • Time types: date, timestamp

Return Result

The return type is the same as the input parameter expr type.

Usage Example

  1. Numeric type comparison:
SELECT greatest(10, 9, 1, 4, -1, null);
-- Return result: 10
  1. String Type Comparison:
SELECT greatest('apple', 'orange', 'banana', null);
-- Return result: 'orange'
  1. Time Type Comparison:
SELECT greatest('2023-01-01', '2022-12-31', null, '2023-02-01');
-- Return result: '2023-02-01'

Notes

  • When all parameters are null, the GREATEST function returns null.