Sunday, 27 September 2015

Difference between Where and Having

Below list is major difference between where and having in SQL Server;


Where
Having
The WHERE clause selects rows before grouping
HAVING clause selects rows after grouping.
Where clause states the criteria which individual records should  meet to be selected by a query , used without the GROUP BY clause. 
HAVING clause cannot be used without the GROUP BY clause.
WHERE clause cannot contain aggregate functions
HAVING clause can contain aggregate functions.
WHERE is used before the aggregation takes place.
HAVING is used to check conditions after the aggregation takes place.
select City, CNT=Count(1)
From Address
Where State = 'MA'
Group By City

select City, CNT=Count(1)
From Address
Where State = 'MA'
Group By City
Having Count(1)>5



No comments:

Post a Comment