PostgreSQL – LEAST Function
The LEAST
function in postgreSQL is used to find the least (smallest) value among a list of expressions. It takes multiple arguments and returns the minimum value from those arguments.
The syntax of the LEAST
function is as follows:
LEAST(expression1, expression2, ..., expressionN)
Here’s an example of how the LEAST
function can be used:
SELECT LEAST(column1, column2, column3) AS min_value FROM table_name;
Suppose you have a table called grades
with columns math_grade
, science_grade
, and english_grade
. You want to find the lowest grade among the three subjects:
SELECT LEAST(math_grade, science_grade, english_grade) AS lowest_grade FROM grades;
The LEAST
function is particularly useful when you want to find the minimum value among multiple values or columns. It’s often used in scenarios where you need to make comparisons and identify the smallest value among a set of values.