MYSQL LEAST Function

MySQL LEAST is one of the Comparison functions, which returns the smallest argument. In this article, we show you how to find the smallest among given values with examples.

This MySQL LEAST function follows the below shown rules:

  • If any of the arguments is NULL, it returns NULL.
  • If all the arguments are integers, it finds the smallest or minimum number.
  • and, If we used strings as the arguments, this function finds the minimum or smallest string.

MYSQL LEAST Function Syntax

The basic syntax of the Least Function is as shown below:

LEAST(Value1, Value2......, ValueN);

MySQL LEAST Function Example

The following query shows multiple ways to use this Least function.

In the First statement, We used integers as arguments. The second statement finds the smallest decimal number. Next, it finds the smallest character in g, d, M, and Z.

SELECT LEAST(22, 5, 10);

SELECT LEAST(12.5, 3.2, 7.0, 2.5, 9.90);

SELECT LEAST('g', 'd', 'M', 'Z');
MySQL LEAST Function Example 1

Here, we used the combinations of integer, string, and decimal inside this Least function. Next, we used NULL as the MySQL argument.

SELECT LEAST('hello', 'hi', 'mysql');

SELECT LEAST(10, 10.0, '10');

SELECT LEAST(10, NULL, 20, 50);
Example 2

MySQL LEAST Function Example 2

W can use this function on column data. This example shows you how to use this function on a table.

In general, we use this function to find the smallest among multiple columns. The below query shows the smallest values among the Yearly Income column and Sales column for each employee Id.

SELECT EmpID,
       FirstName,
       LastName,
       YearlyIncome,
       Sales, 
       LEAST(YearlyIncome, SALES)
 FROM customer
Customer Table 3

The above Query displayed Sales values as output because those are the smallest among the two. This time we are comparing or finding the smallest among the Education and Occupation column values for each Employee ID.

SELECT EmpID, 
       FirstName,
       LastName,
       Education,
       Occupation,
       YearlyIncome,
       Sales,
       LEAST(Education, Occupation)
 FROM customer
MySQL LEAST Function 4