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
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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
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