Here, you will learn how to use the MAX() function to get the maximum value from a record.
MAX() function returns the maximum 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.
For the MAX() examples, we will use the same sample table as in the MIN() section.
We have a movies table that stores the movie details, such as the movie’s title, category, and IMDb rating.
The above query will show the following table:
#Case 1: MAX() with a single expression
For example, you might want to know what is the highest rating among all stored movies:
It will return the following output:
#Case 2: MAX() with GROUP BY clause
We use a MAX() for this example to get the highest rating in each movie category.
It will display the highest rating from a group of movieCategory as shown below:
#Case 3: MAX() in a subquery
In this example, we want to get a movie that has the highest rating by using a subquery:
It will return the following result: