In Sql Server, we have group by clause for grouping the records of the database table according to our need. We use having clause to filter data that we get from group by clause.Having clause operates only on group by clause means to use having clause we need to use group by clause first. Lets go through both the clauses.
Group By Clause
Group By clause is used for grouping the records of the database table(s).This clause creates a single row for each group and this process is called aggregation. To use group by clause we have to use at least one aggregate function in Select statement. We can use group by clause without where clause.
Syntax for Group By Clause
- SELECT Col1, Col2, Aggreate_function
- FROM Table_Name
- WHERE Condition
- GROUP BY Col1, Col2
Let's see how the Group By clause works. Suppose we have a table StudentMarks that contains marks in each subject of the student.
- Create table StudentMarks
- (
- st_RollNo int ,
- st_Name varchar(50),
- st_Subject varchar(50),
- st_Marks int
- )
- --Insert data in StudentMarks table
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(1,'Mohan','Physics',75);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(1,'Mohan','Chemistry',65);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(1,'Mohan','Math',70);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks) values(2,'Vipul','Physics',70);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(2,'Vipul','Chemistry',75);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks) values(2,'Vipul','Math',60);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(3,'Jitendra','Physics',85);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(3,'Jitendra','Chemistry',75);
- insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
- values(3,'Jitendra','Math',60);
- --Now see data in table
- select * from StudentMarks
- -- Group By clause without where condition
- SELECT st_Name, SUM(st_Marks) AS 'Total Marks'
- FROM StudentMarks
- GROUP BY st_Name;
- -- Group By clause with where condition
- SELECT st_Name, SUM(st_Marks) AS 'Total Marks'
- FROM StudentMarks
- where st_Name='Mohan'
- GROUP BY st_Name;
- -- Group By clause to find max marks in subject
- SELECT st_Subject,max(st_Marks) AS 'Max Marks in Subject'
- FROM StudentMarks
- GROUP BY st_Subject;
Having Clause
This clause operates only on group rows of table(s) and act as a filter like as where clause. We use having clause to filter data that we get from group by clause. To use having clause we need to use group by clause first.
- -- Having clause without where condition
- SELECT st_Name, SUM(st_Marks) AS 'Students Scored > 205'
- FROM StudentMarks
- GROUP BY st_Name
- HAVING SUM(st_Marks) > 205
- -- Having clause with where condition
- SELECT st_Name, SUM(st_Marks) AS 'Students Scored > 205'
- FROM StudentMarks
- where st_RollNo between 1 and 3
- GROUP BY st_Name
- HAVING SUM(st_Marks) > 205
Note
- To use Group By Clause, we need to use at least one aggregate function
- All columns that are not used by aggregate function(s) must be in the Group By list
- We can use Group By Clause with or without Where Clause.
- To use Having Clause, we have to use Group By Clause since it filters data that we get from Group By Clause
Summary
In this article I try to explain Group By and Having Clause. I hope after reading this article you are familiar with Group By and Having Clause. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.
No comments:
Post a Comment