Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
mysql
least
communitycreator

What is LEAST() in SQL?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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');

RELATED TAGS

sql
mysql
least
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring