SQL MAX Function

The SQL Server MAX Aggregate Function finds the Maximum value from the total rows or records selected by the SELECT Statement, and the syntax is

SELECT MAX ([Column_Name])
FROM [Source]

The SQL MAX Function will ignore Null values while finding the maximum value. For this demonstration, use the below data.

Customer Table 1

SQL Server MAX Function Example

The Max function 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 Server MAX Function Using on a Single Column 2

Multiple columns

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

SELECT MAX([YearlyIncome]) AS [Maximum Income]
 ,MAX(Sales) AS [Maximum Sale]
FROM [Customer]
Finding for Multiple Columns 3

SQL MAX function Group By Clause

We usually check the maximum product price (Highest pricing product) belonging to a particular category or color, etc. In these situations, we use the 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 MAX Group By clause function Example.

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

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

SQL Max Group By Function 4

SQL MAX Function Order By Clause

You can use it in the Order By Clause. The below query will find the highest YearlyIncome, and Sale value grouped by Education. Next, the Order By clause will sort those query results 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
Order By Example 5

MAX function in Where Clause

You can also use the Where Clause along with it. In this example, we use the 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 along with Where and Order By Clause 6

SQL Server Max Function in Having Clause

When we are grouping the data, Sometimes 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 yearly income, 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 each Group’s highest yearly income is higher than 60000 or not. If this is true, the corresponding records will display.

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

Subquery

This SQL Server Max aggregate function allows you to use it inside a Subqueries in the Where Clause for maximum. 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 example, see that the Maximum Income is 125000. So above query will display the customers whose yearly income is equal to 125000

SQL Max subquery Function 8
Categories SQL