SQL Max Count is used to return the maximum records value of the specified column in a table.
Understand with Example
The Tutorial illiustrate an example from 'SQL Max Count'. To elaborate example we create a table 'Stu'. The create table create a table 'Stu' with required fieldnames and datatypes respectively.
Create table Stu
Create Table Stu(Id int,Name Varchar(15),Marks int); |
Insert values into Stu
The insert into is used to add the records or rows into table 'Stu'.
Insert Into Stu values(1,'Ajay',89); Insert Into Stu values(2,'Bhanu',67); Insert Into Stu values(4,'Rakesh',67); Insert Into Stu values(5,'Santosh',90); Insert Into Stu values(3,'Komal',78); Insert Into Stu values(1,'Ajay',45); Insert Into Stu values(2,'Bhanu',67); Insert Into Stu values(4,'Rakesh',87); Insert Into Stu values(5,'Santosh',40); Insert Into Stu values(3,'Komal',71); |
Stu Table
+------+---------+-------+ | Id | Name | Marks | +------+---------+-------+ | 1 | Ajay | 89 | | 2 | Bhanu | 67 | | 4 | Rakesh | 67 | | 5 | Santosh | 90 | | 3 | Komal | 78 | | 1 | Ajay | 45 | | 2 | Bhanu | 67 | | 4 | Rakesh | 87 | | 5 | Santosh | 40 | | 3 | Komal | 71 | +------+---------+-------+ |
Query for Max Count
The Query given below is used to sort the records by id and returns the maximum value of the marks from table 'stu'.
select id, name ,max(marks) from stu group by id; |
Result
+------+---------+------------+ | id | name | max(marks) | +------+---------+------------+ | 1 | Ajay | 89 | | 2 | Bhanu | 67 | | 3 | Komal | 78 | | 4 | Rakesh | 87 | | 5 | Santosh | 90 | +------+---------+------------+ |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: SQL Max Count View All Comments
Post your Comment