Group by…..Having Clause

SQL Basic SQL Server

Group By:- Group By clauses is used to groups rows based on the distinct values of the specified columns.

The syntax of the Group by clause is:-
  Select column1, column2, column3, aggregate_function(expression )
           From TableName Where (condition)
           Group By Column1, column2, column3

For Example, suppose we have a table named EmpInfo which contains the information about the Id of the employee and the Id of the project and it contains the following data

Table:- EmpInfo
EmployeeId  Projectid
————————–
1                          1
2                          2
3                          2
4                          2
5                          3
Now suppose we want to know about the number of employee belonging to each project then we can use the group by clause as given below:-
select Projectid , count(employeeid) as NumberOfEmployee  from  EmpInfo  Group by ProjectId
This query will give us the following result:-
ProjectId     NumberOfEmployee
1                    1
2                    3
3                    1
Having Clause :- Having clause is used in conjunction with the group clause by imposing a condition on the group by clause to further filter the records return by the group by clause.
 
Syntax for Having Clause:-
       Select column1, column2, column3, aggregate_function(expression )
       From TableName Where (condition)
       Group By Column1, column2, column3
       having aggregate_function(expression )  operator value
For example, suppose we want to know which project has more than 2 employee and the total no of their respective employees, we can use the following query
select Projectid , count(employeeid) as NumberOfEmployee  from  EmpInfo  Group by ProjectId having count(employeeid) >2

The query will give us the following result:-

 ProjectId  NumberOfEmployee
2                    3

In the above example, the Having clause further filter the result return by the Group By clause by imposing a condition that only those projectid will be shown who has more than 2 employee.

DMCA.com

52 thoughts on “Group by…..Having Clause

  1. Pingback: gazebo
  2. Pingback: jobcompass.com
  3. Pingback: click site
  4. Pingback: Totojitu
  5. Pingback: magnetic wallpaper
  6. Pingback: 바카라사이트
  7. Pingback: 카지노
  8. Pingback: 메이저사이트
  9. Pingback: cardloan
  10. Pingback: movies
  11. Pingback: movies online
  12. Pingback: karan johar
  13. Pingback: Top Movies
  14. Pingback: Movies1
  15. Pingback: 11 10 2019
  16. Pingback: w88top.com
  17. Pingback: Serial smotret
  18. Pingback: kinokrad
  19. Pingback: kinokrad 2020
  20. Pingback: casino
  21. Pingback: filmy-kinokrad
  22. Pingback: kinokrad-2019
  23. Pingback: serial
  24. Pingback: cerialest.ru
  25. Pingback: dorama hdrezka
  26. Pingback: movies hdrezka
  27. Pingback: HDrezka
  28. Pingback: kinosmotretonline
  29. Pingback: LostFilm HD 720

Leave a Reply