SQL SELECT MAX

The MAX is an aggregate function in Structured Query Language that returns the largest value from the multiple values of the column in the table.

Syntax of SQL Max() Function

We can also use the WHERE condition for selecting the maximum value from the specific records.

Two Examples of SQL Max Function

In this article, you will learn how to use the MAX function in SQL by the following two examples.

Example 1: This example describes how to use the MAX function in SQL.

Let's create a simple table on which the MAX function is to be run.

The following CREATE TABLE statement creates the Cars_Details table with five fields:

The following INSERT query inserts the records of cars into the Cars_Details table:

The following SELECT query shows the data of the Cars_Details table:


Car_NumberCar_NameCar_AmountCar_Price
2578Creta3900000
9258Audi21100000
8233Venue6900000
8990Nexon7700000
9578Mercedes68000000
1258Thar21500000
2564Jaguar46000000
2155Scorpio81800000

The following query shows the largest Car_Number from the above Car_Details table:

Output:

SQL SELECT MAX

Example 2: This example describes how to use the MAX function with the WHERE clause in the SELECT statement:

The following query creates the College_Student_Details table using the CREATE TABLE statement:

The following SQL queries insert the record of students into the above table using INSERT INTO statement:

Let's see the record of the above table using the following SELECT statement:


Student_IDStudent_NameStudent_CourseStudent_AgeStudent_Marks
101AnujB.tech2088
102RamanMCA2498
104ShyamBBA1992
107VikashB.tech2078
111MonuMBA2165
114JonesB.tech1893
121ParulBCA2097
123DivyaB.tech2189
128HemantMBA2390
130NidhiBBA2088
132PriyaMBA2299
138MohitMCA2192

The following MAX query displays the highest marks under 80 from the above Student_Details table:

Output:

SQL SELECT MAX

MAX Function with SQL GROUP BY clause

The MAX Function with GROUP BY clause shows the highest value in each group from the table.

The syntax to use Group BY Clause with MAX Function is given below:

Example of MAX Function with GROUP BY Clause

Let's take the above College_Student_Details table to understand the concept of MAX function With GROUP BY Clause.

The following query shows the highest marks of student in each course from the above College_Student_Details table:

Output:

Student_CourseAVG (Student_Marks)
B.tech93
MCA98
BBA92
MBA99
BCA97

Next TopicSQL ADD COLUMN




Latest Courses