MIN() vs LEAST() in MySQL: What’s the Difference?

In MySQL, the difference between the MIN() and LEAST() functions is exactly the same as the difference between the MAX() and GREATEST() functions.

In each case, both functions perform a similar operation, but they do have a different syntax.

Syntax

The syntax for MIN() and LEAST() goes like this:

MIN([DISTINCT] expr) [over_clause]
LEAST(value1,value2,...)

So the MIN() function accepts the DISTINCT keyword as well as an OVER clause (and the LEAST() function doesn’t).

But the main difference between these two functions is in the argument/s they accept. Specifically:

  • MIN() accepts one argument
  • LEAST() accepts multiple arguments

So MIN() is typically used to return the minimum value in a column in a database. The table could contain many rows, but this function returns the one with the minimum value.

LEAST() on the other hand, returns the minimum-valued argument from the list of arguments passed to it. So you could pass say, 3 arguments to this function and it will return the one with the smallest value.

Example 1 – The MIN() Function

Here’s an example to demonstrate the MIN() function.

SELECT MIN(Population) AS 'Result'
FROM City;

Result:

+--------+
| Result |
+--------+
|     42 |
+--------+

This example finds the city with the smallest population from the City table. The column that contains the population for each city is called Population.

The main point about this example is that only one argument was supplied to the function, but multiple rows were queried.

If you try to pass multiple arguments to the MIN() function you’ll get an error.

Example 2 – The LEAST() Function

Here’s an example to demonstrate the LEAST() function.

SELECT LEAST(1, 5, 9) AS 'Result';

Result:

+--------+
| Result |
+--------+
|      1 |
+--------+

So in this case, we provide three arguments. Each argument is compared against the other. This is in contrast to the single argument provided to the MIN() function.

If you try to pass a single argument to the LEAST() function you’ll get an error.