MariaDB LEAST() Function

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

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

MariaDB LEAST() Syntax

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

LEAST(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 'LEAST'.

Return value

The MariaDB LEAST() function returns the minimum value in the argument list.

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

MariaDB LEAST() Examples

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

Numbers

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

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

Output:

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

Strings

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

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

Output:

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

Dates

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

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

Output:

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

NULL parameters

If any parameter is NULL, the LEAST() function will return NULL.

SELECT LEAST(1, 2, NULL);

Output:

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

Conclusion

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