At the end of this article, you will be able to use the MIN() function to get the minimum value from a set of values.
MIN() function returns the minimum value from a set of records.
The input and return types we support can be seen in the table below.
✅ The return will be the same as the data type used as the input values.
- Returns NULL if there are no input rows or NULL values.
- Returns NaN if the input contains a NaN.
We have a movies table that stores the movie details, such as the movie’s title, category, and IMDb rating:
It will create a table as shown below:
#Case 1: MIN() with a single expression
For example, you might want to know what is the lowest rating of all stored movies:
It will return the following output:
#Case 2: MIN() with GROUP BY clause
For this example, we use a GROUP BY clause to group the movie categories, then use MIN() to get the lowest rating in each movie category.
It will display the lowest rating from a group of movieCategory as shown below:
#Case 3: MIN() in a subquery
In this example, we can get a movie that has the lowest rating by using a subquery:
- First, the subquery will select the lowest rating using a MIN() function and store it as the IMDbRating.
- Then, the outer query selects the movie with a rating equal to the lowest rating stored in the IMDbRating.
It will return the following result: