Function Reference

GREATEST

Returns the largest of the given values.

MySQL 8.1
GREATEST(value, value[, ...])

Parameters

value
Value to compare: number, string or date

Return value

The largest value; NULL if any argument is NULL

Examples

Largest of three numbers

MySQL 8.1
SELECT GREATEST(1, 2, -1);
GREATEST(1, 2, -1)
2

Largest of three strings

MySQL 8.1
SELECT GREATEST('apple', 'banana', 'cherry');
GREATEST('apple', 'banana', 'cherry')
cherry

Comparison with NULL

MySQL 8.1
SELECT GREATEST(1, NULL, 3);
GREATEST(1, NULL, 3)
<NULL>

Details

Unlike the aggregate function MAX, which finds the maximum across table rows, GREATEST compares several values within one row. If the arguments mix numbers and strings, they are compared as strings: GREATEST(2, '10') returns '2'.

See also