Oracle LEAST() Function

Oracle LEAST() is a built-in function that returns the minimum value from a given list of parameters.

To obtain the minimum value from a list of parameters, use LEAST().

Oracle LEAST() Syntax

Here is the syntax for the Oracle LEAST() function:

LEAST(expr [, expr ]...)

Parameters

expr [, expr ]...

Required. The list of parameters used for comparison. All parameters participate in the comparison. Parameters can be of any data type or expression. You should provide at least one parameter.

Return Value

The Oracle LEAST() function returns the minimum value from the given list of parameters.

The Oracle database uses the first expr to determine the return type. If the first expr is a number, then Oracle determines the parameter with the highest numeric priority, implicitly converts all remaining parameters to that data type before comparison, and returns that data type. If the first expr is not a number, then after comparison, each expr after the first expr is implicitly converted to the data type of the first expr.

If any parameter is NULL, LEAST() returns NULL.

Oracle LEAST() Examples

Here are several examples that demonstrate the usage of the Oracle LEAST() function.

Numbers

To obtain the minimum value from a list of numbers, use the following statement:

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

Output:

   LEAST(1,4,2,5,3)
___________________
                  1

Strings

The MariaDB LEAST() function supports strings as parameters and returns the smallest string.

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

Output:

LEAST('ABC','HELLO','GOOD')
______________________________
abc

Dates

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

SELECT
    LEAST(DATE '2023-01-31', DATE '2023-01-01') Result
FROM dual;

Output:

RESULT
___________
01-JAN-23

NULL Parameters

If any parameter is NULL, LEAST() returns NULL.

SET NULL 'NULL';
SELECT
    LEAST(NULL, NULL) Result1,
    LEAST('ab', NULL) Result2,
    LEAST(NULL, 'ab') Result3
FROM dual;

Output:

RESULT1    RESULT2    RESULT3
__________ __________ __________
NULL       NULL       NULL

In this example, we use the SET NULL 'NULL'; statement to display the NULL values as the string 'NULL'.

Conclusion

Oracle LEAST() is a built-in function that returns the minimum value from a given list of parameters.