What is LEAST() in SQL?

The LEAST() function returns the smallest value from a list of arguments.

Figure 1 shows a visual representation of the LEAST() function.

Figure 1: Visual representation of LEAST() function

Syntax

LEAST(arg1, arg2, .... argn)

Parameter

The LEAST() function takes n number of arguments as a parameter.

Arguments can be of any data type, such as integer, float, string, etc.

Return value

The LEAST() function returns the smallest value from the list of arguments sent as a parameter.

  • If any argument is NULL, then this function will return NULL.
  • If the arguments are a mixture of integers and strings, then this function will compare them as numbers.
  • If any argument is a non-binary string, then this function will compare all the arguments as non-binary strings.
  • If two or more arguments have the smallest value, then this function will return the first occurring smallest value.

Code

-- NULL as argument
SELECT LEAST(NULL,10,3,4,6,9);
-- integers as argument
SELECT LEAST(5,10,3,4,6,9);
-- floats as arguments
SELECT LEAST(4.9,4.6,4.5,4.2,4.1);
-- strings as argument
SELECT LEAST('educative', 'edpresso');
-- mixture of strings and integer
-- first '10' and '11' will be casted as integer and then comparison is done
SELECT LEAST('10',1,'11');

Free Resources