Group By:- Query involving Group by Clause must Satisfied one of the two below Condition:-
1) Column must contain aggregate function
2) Column has to be part of group by clause
For e.g.
1) Column must contain aggregate function
2) Column has to be part of group by clause
For e.g.
Where Clause Vs Having Clause Filter:-
1) Where Clause:-
For e.g.
--Query with where clause filter
select COUNT(LoginID) as [Login],[JobTitle],[MaritalStatus]
from [AdventureWorks2008].[HumanResources].[Employee]
where [MaritalStatus] = 'M'
group by [JobTitle],[MaritalStatus]
Here above query retrieve only those rows whose MartalStatus is 'M' and then apply Group by filter.
2) Having Clause:-
For e.g.
--Query with having clause filter
select COUNT(LoginID) as [Login],[JobTitle],[MaritalStatus]
from [AdventureWorks2008].[HumanResources].[Employee]
group by [JobTitle],[MaritalStatus]
having [MaritalStatus] = 'M'
Here above query retrieve all the rows and group them by [JobTitle] & [MaritalStatus] and then yield only those rows whose [MaritalStatus] is 'M'.
For e.g.:-
Where Vs Having Clause:-
1) Where clause filter the rows before grouping i.e. aggregation is performed,where as having clause does after the grouping is performed.
2) Where clause can be used with insert,update and delete statement,where as having clause is only used with select statement.
Note:-Aggregate function can't be used in the where clause,where as in the having clause we can used aggregate function.
No comments:
Post a Comment