MAX Function in SQL

The MAX is an aggregate function in SQL which returns the maximum or largest value from the specified column of the table.

Syntax of MAX Function

In the Structured Query Language, we use the MAX function with the columns of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the MAX function.

Example of MAX function

Here, we will create the new table through which we will perform the MAX function with the columns of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:


Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateCategoryProduct_Rating
104P110.250945NULL2022-04-30ClothsNULL
202P415.50045752022-01-28Electrical5
103P218.25025NULL2022-02-18Toys4
111P725.2505152021-12-25Cloths9
210P615.50050702021-10-15ElectricalNULL
212P819.750110250 2022-01-28Cloths4
112P1010.2505508352022-04-11ToysNULL

Query 1: The following SELECT query uses the MAX function with the Product_Quantity column of the above Product_Details table:

This query shows the largest value of product quantity column.

Output:

Maximum_in_productquantity
25.250

Query 2: The following SELECT query uses the MAX function with the Selling_Price column of the above Product_Details table:

This query returns the maximum value of selling price products.

Output:

Maximum_in_sellingpriceproducts
835

Query 3: The following SELECT query uses the MAX function with the Product_Rating column of the above Product_Details table:

This query returns the largest values of Product_rating columns.

Output:

Maximum_in_productrating
9

Query 4: The following SELECT query uses the MAX function with the addition of two columns of the above Product_Details table:

This query returns the largest values from the addition of purchasing and selling price.

Output:

Maximum_of_Purchasing+Selling
1385

MAX Function with WHERE clause

We can also use the WHERE clause with the MAX function which returns the largest value from the filtere rows.

The syntax for using the MAX function with the WHERE clause is as follows:

Query 1: The following SELECT query uses the MAX function with WHERE clause in the above Product_Details table:

This query returns the largest quantity from those products whose product id is greater than 200.

Output:

Maximum_in_product>200
19.750

Query 2: The following SELECT query uses the MAX function with WHERE clause in the above Product_Details table:

This query returns the largest purchasing price from those products which are release on 2022-01-28.

Output:

Maximum_in_purchasingprice
110

MAX Function with GROUP BY clause

We can also use the GROUP BY clause with the MAX function which returns the maximum value from the same group.

The syntax for using the MAX function with the GROUP BY clause is as follows:

Query 1: The following SELECT query uses the MAX function with the GROUP BY clause in the above Product_Details table:

This query returns the maximum value from each specified group.

Output:

CategoryMaximum_in_samegroup
Cloths25.250
Toys18.250
Electrical15.500