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 Server Max is
SELECT MAX ([Column_Name]) FROM [Source]
The Sql Server MAX Function will ignore Null values. For this SQL Server Max function demonstration, use the below data
SQL MAX 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.
-- SQL Server MAX Function example USE [SQL Tutorial] GO SELECT MAX([YearlyIncome]) AS [Maximum Income] FROM [Customer]
SQL MAX Multiple columns
We can also use Sql Server MAX function in multiple columns. In this example, we are finding the Maximum Sales value and the maximum Yearly Income.
-- SQL Server MAX Example SELECT MAX([YearlyIncome]) AS [Maximum Income] ,MAX(Sales) AS [Maximum Sale] FROM [Customer]
SQL 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 the SQL Max Function to find the Highest pricing product present in each group. Let us see the MAX Group By Example
-- SQL Server MAX 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 Order By Clause
You can use the SQL Max function in the Order By Clause. The below query will find the Maximum Yearly income, and Sale value grouped by the Education. Next, the SQL Server Order By clause will sort those query result based on the Maximum of yearly income in descending order.
-- SQL Server MAX Example SELECT Education ,MAX([YearlyIncome]) AS [Maximum Income] ,MAX(Sales) AS [Maximum Sale] FROM [Customer] GROUP BY Education ORDER BY MAX([YearlyIncome]) DESC
SQL MAX Where Clause
You can also use Where Clause along with the SQL Max function. In this example, we use where clause to display the sales greater than 1000
-- SQL Server MAX Function example 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 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 SQL server max query will group the Customers by their Education & Occupation. Then finds the Maximum yearly Income, Sales present in each group.
-- SQL Server MAX Example SELECT Education ,Occupation ,MAX([YearlyIncome]) AS [Maximum Income] ,MAX(Sales) AS [Maximum Sale] FROM [Customer] GROUP BY Education, Occupation HAVING MAX([YearlyIncome]) > 60000
It checks whether the Maximum 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 Subquery
The Sql Server Max 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.
-- SQL Server MAX Function example SELECT [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer] WHERE [YearlyIncome] = (SELECT MAX([YearlyIncome]) FROM Customer)
From the first SQL Maximum function example, see that Maximum Income is 125000. So above query will display the customers whose yearly income is equal to 125000