LEAST
LEAST is a SQL function that returns the smallest value among a list of expressions. It compares each value and outputs the minimum one. The return type of LEAST is determined by the context in which it is used.
LEAST(value1, value2, …);
- value1: The first numeric or string value from which the function should calculate the least value.
- value2: The second numeric or string value from which the function should calculate the least value.
- …: This represents an optional series of additional numeric or string values. Each additional value is considered in the computation of the least value.
Example
Output
Explanation
The LEAST function in MySQL returns the smallest of the given values. In the provided example, it compares 34, 17, and 68 and returns 17 since it is the smallest value among the three.
LEAST(value1, value2, …)
- value1: The first numerical value, or date/time/timestamp string to be compared in the LEAST function. This is a required parameter.
- value2: The second numerical value, or date/time/timestamp string to be compared in the LEAST function. This is also a required parameter.
- …: Additional numerical values, or date/time/timestamp strings that can be included in the LEAST function. These are optional parameters. The function will return the smallest value among all the provided parameters.
Example
Output
Explanation
The LEAST function in PostgreSQL returns the smallest value of the list of arguments. In the provided example, the function returns 2, which is the smallest number in the list (9, 3, 6, 2).
LEAST(expr1, expr2, …, exprn)
- expr1: The first expression to compare. This can be a column name, a subquery returning a single value, or a constant.
- expr2: The second expression to compare. Like expr1, it can also be a column name, a subquery returning a single value, or a constant.
- …, exprn: Additional expressions to compare. Any number of expressions can be added, separated by commas. These also can be column names, subqueries returning a single value, or constants.
Example
Output
Explanation
The LEAST
function in Oracle returns the smallest value from the list of values. In the provided example, it returns 6 as it is the smallest among the values 15, 6, 9.
LEAST(expression1, expression2 [, …n ])
- expression1: This mandatory argument specifies the first value to be evaluated for the LEAST function. It can be an SQL expression such as a column name, literal value, or mathematical operation between values.
- expression2: This mandatory argument specifies the second value to be compared with expression1 by the LEAST function. Like the first expression parameter, it can also be SQL expressions.
- …n: These are optional arguments and can be used to input any desired number of additional values. The function will return the lesser from all the values (expression1, expression2, …, n). This parameter operates same way as expression1 and expression2.
Example
Output
Explanation
In this example, the LEAST
function is used to select the smallest value from the set of given numbers. The output is 3
as it is the smallest number among 3
, 7
, 5
, 9
, and 11
.