Updated March 8, 2023
Introduction to SQL MAX()
SQL MAX() is one of the aggregate functions available in SQL that helps us fetch the greatest value among multiple values specified in the column values of records, the expression consisting of the column that is mentioned. When a query is used to retrieve the data that report related and contains a group by a statement, the MAX() function is used to get the greatest value of a particular column or columns based on the grouping function.
Syntax and Usage
The syntax of the MAX function in SQL is given below:
SELECT MAX(expression) FROM table_name [WHERE restriction];
Where expression can be any name of the column of the table or a formula built up using column names and static literal values or variables, the table_name is the name of the table from which you want to retrieve the records and calculate the greatest value from one of their columns. The use of the FROM table name clause is required. One optional thing is the use of a where clause to mention the conditions and restrictions that the records of the table should fulfil to consider that record’s column value for fetching the greatest value.
When where clause is used, only filtered out data is considered for the greatest calculation by MAX() function. The MAX() function collects all the values of the expression mentioned in it and adds them up, which is further divided by the number of values or expressions that were added to find out the final greatest value. For example, consider that we have to find out a greatest of 50, 100, 150, and 200. Then the max function will internally compare each of their values and finally derive 200 as the greatest maximum value.
Examples of SQL MAX()
Given below are the examples of SQL MAX():
Example #1 – Using a single column.
Let us firstly consider a simple example that we used above. We will calculate the greatest value of SQL numbers using the MAX() function. Let us create one simple table named numbers and store the num column value in it.
We will use the following query statement to create our table.
CREATE TABLE numbers (num INT) ;
Now, we will insert the above records in the table.
INSERT INTO numbers(num) VALUES (50), (100), (150), (200);
Let us now retrieve the records once.
SELECT * FROM numbers ;
Now, we will calculate the greatest of num column of numbers table using MAX() function using the following query statement.
SELECT MAX(num) FROM numbers ;
Example #2 – Using the distinct function.
We can use the distinct function in MAX() function to consider the column’s repetitive values only once while fetching the greatest value. Suppose that we insert some more records in the numbers table using the following query statement.
INSERT INTO numbers(num) VALUES (350), (800), (150), (300),(450), (100), (250); select * from numbers;
If we use SELECT MAX(num) FROM numbers; statement to calculate the greatest value of num column, then each of the values will be considered while fetching the greatest value.
SELECT MAX(DISTINCT(num)) FROM numbers ;
The output will be the same as that of the first query without a distinct function but internally the calculation of greatest value by MAX() function will only consider the column values that are repeated such as 100 and 150 only once.
The output of both of the above queries is as shown below.
Example #3 – Using formula.
We can use the MAX() function expressions to consider the value evaluated by fetching each of the formula or expression values containing column value to calculate the greatest value.
Let us consider one example; we will calculate the greatest of all the columns after they are multiplied by 10 and added by 1.
SELECT MAX((num * 10) + 1) FROM numbers ;
We can even use the existing functions such as SUM() and COUNT() inside the MAX() function.
Example #4 – Using group by.
When we have complex tables and relations between multiple tables, we have to query those tables using joins to retrieve data, usually for reporting purposes that consist of summarized data. Even in some scenarios, the data from a single table need to be manipulated to get summarized data. Suppose that we have one table named workers consisting of the following records in it that are retrieved by executing a simple select query on that table.
SELECT * FROM workers;
Now, the situation is such that we have to calculate the greatest salary of the workers per team. The output should consist of the team id and the greatest salary of that team. For this, we will have to use the group by statement and group the records based on team id and calculate the greatest salary by using MAX() function.
SELECT team_id, MAX(salary) FROM workers GROUP BY team_id ;
The execution of the above query statement will give the following output along with team ids and their respective greatest salaries.
We can use the MAX() function in SQL to fetch the greatest value of the columns of the tables or greatest of expressions that involve column values and even calculate the greatest value of columns in the grouped manner using the GROUP BY statement.
We hope that this EDUCBA information on “SQL MAX()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.