Group by…..Having Clause

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

About vivekjohari

Database Consultant with more than 11.5 years of experience in database designing & programming and DBA related activities.  Had good experience on different databases like SQL Server, MySQL & Oracle, Azure SQL &  Big Data.
This entry was posted in SQL Basic, SQL Server and tagged . Bookmark the permalink.

71 Responses to Group by…..Having Clause

  1. Anonymous says:

    thank you

  2. Pingback: cars for sale in durban

  3. Pingback: agencia de viajes en managua

  4. Pingback: gazebo

  5. Pingback: agencia de viajes en managua

  6. Pingback: jobcompass.com

  7. Pingback: agencia de viajes en managua

  8. Pingback: p385#https://bizhub24.pl

  9. Pingback: Feed store near me 281-471-1171

  10. Pingback: Free Gold Silver Stock Research 1 Bought by The Rothss Childss

  11. Pingback: Business Analysis Shows Sexy Sex Doll is Potential Growth Worldwide

  12. Pingback: click site

  13. Pingback: Legit website developer in the USA

  14. Pingback: 팩스 프린터 렌탈

  15. Pingback: ต้องการ ที่ดิน ด่วน

  16. Pingback: Totojitu

  17. Pingback: Disponibilidad en Santo Domingo y a Nivel Nacional

  18. Pingback: magnetic wallpaper

  19. Pingback: 바카라사이트

  20. Pingback: 카지노

  21. Pingback: How to Earn Free Avios

  22. Pingback: https://www.wasseem.org/

  23. Pingback: 메이저사이트

  24. Pingback: Bathroom fitter Wakefield

  25. Pingback: Free Email Marketing Software 2019

  26. Pingback: cardloan

  27. Pingback: ธนาธร จึงรุ่งเรืองกิจ

  28. Pingback: movies

  29. Pingback: movies online

  30. Pingback: karan johar

  31. Pingback: Top Movies

  32. Pingback: Movies1

  33. Pingback: 11 10 2019

  34. Pingback: w88top.com

  35. Pingback: smotretonline2015.ru

  36. Pingback: Serial smotret

  37. Pingback: kinokrad

  38. Pingback: kinokrad 2020

  39. Pingback: ñïèñîê íîâûõ ôèëüìîâ

  40. Pingback: Watch Tv Series Online

  41. Pingback: casino

  42. Pingback: Kinokrad 2019 Kinokrad Hd

  43. Pingback: filmy-kinokrad

  44. Pingback: kinokrad-2019

  45. Pingback: filmy-2019-kinokrad

  46. Pingback: serial

  47. Pingback: cerialest.ru

  48. Pingback: dorama hdrezka

  49. Pingback: movies hdrezka

  50. Pingback: HDrezka

  51. Pingback: kinosmotretonline

  52. Pingback: LostFilm HD 720

  53. Pingback: trustedmdstorefy.com

  54. Pingback: bofilm ñåðèàë

  55. Pingback: bofilm

  56. Pingback: 1 seriya

  57. Pingback: Êîíñóëüòàöèÿ ïñèõîëîãà

  58. Pingback: topedstoreusa.com

  59. Pingback: hqcialismht.com

  60. Pingback: viagramdtrustser.com

  61. Pingback: lindamedic.com

  62. Pingback: 4serial.com

  63. Pingback: See-Season-1

  64. Pingback: Evil-Season-1

  65. Pingback: Evil-Season-2

  66. Pingback: Evil-Season-3

  67. Pingback: Evil-Season-4

  68. Pingback: Dollface-Season-1

  69. Pingback: Queer-Eye-We-re-in-Japan-Season-1

  70. Pingback: 2020

  71. Pingback: koma 2020

Leave a Reply