MySQL LEAST() Function
The LEAST()
function in MySQL compares multiple values and returns the smallest one. It's a handy function for finding minimum values within a set of numbers or strings.
LEAST(): Definition and Usage
LEAST()
is useful for identifying the minimum value among a list of arguments. It can work with numeric and string data types. For strings, the comparison is lexicographical (alphabetical).
Related Function
The GREATEST()
function finds the largest value in a list, performing the opposite operation to LEAST()
.
Syntax
Syntax
LEAST(arg1, arg2, arg3, ...)
Parameter Values
Parameter | Description |
---|---|
arg1, arg2, arg3, ... |
The values to compare. At least one argument is required. |
Examples
Finding the Smallest Number
This example finds the smallest number among a list of numbers.
Syntax
SELECT LEAST(3, 12, 34, 8, 25);
Output
3
Finding the Smallest String (Lexicographical Order)
This example finds the smallest string alphabetically.
Syntax
SELECT LEAST("w3Schools.com", "microsoft.com", "apple.com");
Output
apple.com