LEAST
Overview
The LEAST()
function returns the least or smallest value in a list of values. It needs at least one argument to work with, and if you mix different types, like a text and a number, it will return an error.
For example, comparing the greatest value among 4, “two”, and 9 would result in an error.
Syntax
The syntax for the LEAST()
function is as follows:
Where:
-
value_1
: Represents the first value. -
value_n
: Represents one or more additional values, separated by commas.
-
NULL
values in the list will be ignored. - The result will be
NULL
if all the expressions evaluate to NULL
.Examples
Below are several examples of the LEAST()
function:
Case #1: Basic Usage
Consider the following example:
The query will return 3
, the smallest value among the provided values.
Case #2: String Comparison
String comparison is also supported, as shown below:
In this case, the result will be 'a'
, as it is the smallest string.
Case #3: Handling NULL Values
NULL
values are ignored when determining the smallest value:
The result will be the smallest non-NULL value, which is 5
.
Case #4: Negative Numbers
Negative numbers can also be compared:
This query will return -8
, the smallest value among the provided numbers.
Case #5: Using Table Data
Suppose we have a table named grades
containing columns x
, y
, and z
.
To find the smallest value among these columns, you can use the following query:
This query will add a new column named least_grade
to the result, displaying the smallest value among columns x
, y
, and z
.