MariaDB GREATEST() Function

In MariaDB, GREATEST() is a built-in function that returns the maximum value from a given list of arguments.

Use LEAST() if you want to get the minimum value in the argument list.

MariaDB GREATEST() Syntax

Here is the syntax of the MariaDB GREATEST() function:

GREATEST(param1, param2, ..., paramN)

Parameters

param1, param2, ..., paramN

Optional. A list of arguments to use for comparison. All parameters are involved in the comparison. Parameters can be any data type, or an expression.

You should provide at least two parameters, otherwise MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'GREATEST'.

Return value

The MariaDB GREATEST() function returns the maximum value from the parameter list.

If either argument is NULL, the GREATEST() function will return NULL.

MariaDB GREATEST() Examples

The following example shows the usage of the MariaDB GREATEST() function.

Numbers

To get the maximum value from some numbers, use the following statement:

SELECT GREATEST(1, 4, 2, 5, 3);

Output:

+-------------------------+
| GREATEST(1, 4, 2, 5, 3) |
+-------------------------+
|                       5 |
+-------------------------+

Strings

The MariaDB GREATEST() function supports strings as parameters, and it returns the largest string among them.

SELECT GREATEST('abc', 'hello', 'good');

Output:

+----------------------------------+
| GREATEST('abc', 'hello', 'good') |
+----------------------------------+
| hello                            |
+----------------------------------+

Date

The MariaDB GREATEST() function allows you to get the largest value in a set of dates.

SELECT GREATEST('2023-01-31', '2023-01-01');

Output:

+--------------------------------------+
| GREATEST('2023-01-31', '2023-01-01') |
+--------------------------------------+
| 2023-01-31                           |
+--------------------------------------+

NULL parameters

If either argument is NULL, the GREATEST() function will return NULL.

SELECT GREATEST(1, 2, NULL);

Output:

+----------------------+
| GREATEST(1, 2, NULL) |
+----------------------+
|                 NULL |
+----------------------+

Conclusion

In MariaDB, GREATEST() is a built-in function that returns the maximum value from a given list of arguments.