Pages

Sunday, May 4, 2014

Definition, Use of Group by and Having Clause

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
  1. SELECT Col1, Col2, Aggreate_function
  2. FROM Table_Name
  3. WHERE Condition
  4. 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.
  1. Create table StudentMarks
  2. (
  3. st_RollNo int ,
  4. st_Name varchar(50),
  5. st_Subject varchar(50),
  6. st_Marks int
  7. )
  8. --Insert data in StudentMarks table
  9. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  10. values(1,'Mohan','Physics',75);
  11. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  12. values(1,'Mohan','Chemistry',65);
  13. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  14. values(1,'Mohan','Math',70);
  15. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks) values(2,'Vipul','Physics',70);
  16. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  17. values(2,'Vipul','Chemistry',75);
  18. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks) values(2,'Vipul','Math',60);
  19. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  20. values(3,'Jitendra','Physics',85);
  21. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  22. values(3,'Jitendra','Chemistry',75);
  23. insert into StudentMarks(st_RollNo,st_Name,st_Subject,st_Marks)
  24. values(3,'Jitendra','Math',60);
  25. --Now see data in table
  26. select * from StudentMarks

  1. -- Group By clause without where condition
  2. SELECT st_Name, SUM(st_Marks) AS 'Total Marks'
  3. FROM StudentMarks
  4. GROUP BY st_Name;

  1. -- Group By clause with where condition
  2. SELECT st_Name, SUM(st_Marks) AS 'Total Marks'
  3. FROM StudentMarks
  4. where st_Name='Mohan'
  5. GROUP BY st_Name;

  1. -- Group By clause to find max marks in subject
  2. SELECT st_Subject,max(st_Marks) AS 'Max Marks in Subject'
  3. FROM StudentMarks
  4. 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.
  1. -- Having clause without where condition
  2. SELECT st_Name, SUM(st_Marks) AS 'Students Scored > 205'
  3. FROM StudentMarks
  4. GROUP BY st_Name
  5. HAVING SUM(st_Marks) > 205

  1. -- Having clause with where condition
  2. SELECT st_Name, SUM(st_Marks) AS 'Students Scored > 205'
  3. FROM StudentMarks
  4. where st_RollNo between 1 and 3
  5. GROUP BY st_Name
  6. HAVING SUM(st_Marks) > 205

Note

  1. To use Group By Clause, we need to use at least one aggregate function
  2. All columns that are not used by aggregate function(s) must be in the Group By list
  3. We can use Group By Clause with or without Where Clause.
  4. 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