We use group by for grouping the records of table. For using group by clause we use at least one aggregate function in select statement. We can use group by clause without where clause.
For Understanding this we take an example
We take a table name it Emp_Info and insert following Records
1.Using group by with where clause
select Emp_Name,sum(salary) as Total_Salary from Test.dbo.Emp_Info where Emp_Name='A' group by Emp_Name
Here we have used where clause it returns the record of Emp_Name A.
2. Without using where clause
select Emp_Name,sum(salary) as Total_Salary from Test.dbo.Emp_Info group by Emp_Name
In the above sql query I have not used the where clause .when we execute above query it return all the Emp_Name with total salary.
Having clause-
It is use for filtering records from the table it work like where clause. It filter records from the data that we get from group by clause
It places the condition on group. So for using having clause we have to use group by clause first.
select Emp_Name,sum(salary) as Total_Salary from Test.dbo.Emp_Info group by Emp_Name having sum(salary)<2300
Having clause solves the drawback of where clause because where clause does not support aggregate function
Liked By
Write Answer
What is the Use of group by and Having Clause
Join MindStick Community
You have need login or register for voting of answers or question.
Manish Kumar
18-Jan-2017We use group by for grouping the records of table. For using group by clause we use at least one aggregate function in select statement. We can use group by clause without where clause.
For Understanding this we take an example
We take a table name it Emp_Info and insert following Records
1.Using group by with where clause
Here we have used where clause it returns the record of Emp_Name A.
2. Without using where clause
In the above sql query I have not used the where clause .when we execute above query it return all the Emp_Name with total salary.
Having clause-
It is use for filtering records from the table it work like where clause. It filter records from the data that we get from group by clause
It places the condition on group. So for using having clause we have to use group by clause first.
Having clause solves the drawback of where clause because where clause does not support aggregate function