MySQL GREATEST() Function

The GREATEST() function in MySQL compares multiple values and returns the largest one. It's a very handy function for finding maximum values within a set of numbers or strings.



GREATEST(): Definition and Usage

GREATEST() is useful for identifying the maximum value among a list of arguments. It works with both numeric and string data types. For strings, the comparison is lexicographical (alphabetical order).

Syntax

Syntax

GREATEST(arg1, arg2, arg3, ...)
      

Parameter Values

Parameter Description
arg1, arg2, arg3, ... The values to compare. At least one argument is required.

Related Function

The LEAST() function returns the smallest value, in contrast to GREATEST() which returns the largest.

Examples

Finding the Greatest Number

This example finds the largest number among a list of numbers.

Syntax

SELECT GREATEST(3, 12, 34, 8, 25);
      
Output

34
      

Finding the Greatest String

This example finds the largest string alphabetically.

Syntax

SELECT GREATEST("w3Schools.com", "microsoft.com", "apple.com");
      
Output

w3Schools.com