SQL MAX Function

The SQL MAX Function is an Aggregate Function that finds the Maximum value from the total rows or records selected by the SELECT Statement. The syntax of the SQL Max Function is

SELECT MAX ([Column_Name])
FROM [Source]

The Sql Server MAX Function will ignore Null values. For this function demonstration, use the below data

SQL Max Function 1

SQL MAX Function Example

The Max function in SQL Server returns the Maximum value from the total records present in the specified column. For example, The following query will find the Maximum income present in the [Yearly Income] column from the Customers table.

SELECT MAX([YearlyIncome]) AS [Maximum Income]
FROM [Customer]
SQL Max Function 2

SQL MAX Multiple columns

We can also use MAX function in multiple columns. In this example, we are finding the Maximum Sales value and the maximum Yearly Income.

SELECT MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
SQL Max Function 3

MAX Group By Clause

We usually check the maximum product price (Highest pricing product) belongs to a particular category or color, etc. In these situations, we use GROUP BY Clause to group the products by color or category and then use this Function to find the Highest pricing product present in each group. Let us see the SQL MAX Group By clause Example

SELECT Education
 ,MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
GROUP BY Education

Above Aggregate Function query groups the customers by their Education qualification. And SELECT the maximum income and sale of the customers present in each education group

SQL Max Function 4

SQL MAX Order By Clause

You can use the Max function in the Order By Clause. The below query will find the highest YearlyIncome, and Sale value grouped by the Education. Next, the SQL Server Order By clause will sort those query result based on the Maximum YearlyIncome in descending order.

SELECT Education
 ,MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
GROUP BY Education
ORDER BY MAX([YearlyIncome]) DESC
SQL Max Function 5

SQL MAX Where Clause

You can also use Where Clause along with it. In this example, we use where clause to display the sales greater than 1000

SELECT Education
 ,MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
WHERE Sales > 1000
GROUP BY Education
ORDER BY MAX([YearlyIncome]) DESC
SQL Max Function 6

SQL Max Having Clause

When we are grouping the data, In some instances, we normally check for the conditions against the aggregated data. In that circumstance, we use the HAVING Clause along with Group By Statement.

For example, the following query will group the Customers by their Education & Occupation. Then finds the highest yearlyIncome, Sales present in each category.

SELECT Education
 ,Occupation
 ,MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
GROUP BY Education, Occupation 
HAVING MAX([YearlyIncome]) > 60000 

The last line of query checks whether the highest Yearly income of each Group is higher than 60000 or not. If this is true, the corresponding records will display.

HAVING MAX ([YearlyIncome]) > 60000
SQL Max Function 7

SQL Max Subquery

This aggregate function allows you to use it inside a Subqueries in Where Clause. It returns the Customers whose [Yearly Income] is exactly equal to the Maximum income.

SELECT [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
  WHERE [YearlyIncome] = (SELECT MAX([YearlyIncome])
 FROM Customer)

From the first SQL example, see that Maximum Income is 125000. So above query will display the customers whose yearly income is equal to 125000

SQL Max Function 8