MySQL LEAST Function Explained

The LEAST function in MySQL is used to compare multiple values and return the smallest value. It accepts one or more parameters and returns the smallest value among them. If the parameter list includes any NULL values, the LEAST function will return NULL.

For example, suppose there is a table named students with two columns math_score and english_score. If we want to query for the lower score among the math scores and English scores for each student, we can use the LEAST function.

SELECT student_name, LEAST(math_score, english_score) AS lower_score
FROM students;

In the example above, LEAST(math_score, english_score) will return the lower score between each student’s math score and English score, and display it as an alias lower_score.

bannerAds