Aggregate
MIN
Overview
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.
Input Type | Return Type |
---|---|
INT | INT |
LONG | LONG |
FLOAT | FLOAT |
DOUBLE | DOUBLE |
TEXT | TEXT |
DATE | DATE |
TIME | TIME |
TIMESTAMP WITHOUT TIMEZONE | TIMESTAMP WITHOUT TIMEZONE |
TIMESTAMP WITH TIMEZONE | TIMESTAMP WITH TIMEZONE |
The return will be the same as the data type used as the input values.
Special cases:
- Returns
NULL
if there are no input rows orNULL
values. - Returns
NaN
if the input contains aNaN
.
Examples
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 theIMDbRating
. - 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: