In recent interview sessions in hiring process I asked this question to every prospect who said they know basic SQL. Surprisingly, none answered me correct. They knew lots of things in details but not this simple one. One prospect said he does not know cause it is not on this Blog. Well, here we are with same topic online.
Answer in one line is : HAVING specifies a search condition for a group or an aggregate function used in SELECT statement.
HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows. A query can contain both a WHERE clause and a HAVING clause. The WHERE clause is applied first to the individual rows in the tables . Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an aggregate function. (Reference :BOL)
Example of HAVING and WHERE in one query:
SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = 'CA'
GROUP BY titles.pub_id
HAVING AVG(titles.price) > 10
Sometimes you can specify the same set of rows using either a WHERE clause or a HAVING clause. In such cases, one method is not more or less efficient than the other. The optimizer always automatically analyzes each statement you enter and selects an efficient means of executing it. It is best to use the syntax that most clearly describes the desired result. In general, that means eliminating undesired rows in earlier clauses.
Reference : Pinal Dave (http://blog.SQLAuthority.com)





Very helpful explanation. Thanks.
Very Nice Explanation, Thanks a Lot.
very nice explaination
Thanks brother, it is a nice example to understand differences between HAVING and WHERE Clause.
No doubt, you explain it very Well
Thanks a lot.. that was quite descriptive..
Thanks Mr. its very usefull.
Regards
Parshu
Thank You. Very usefull explanation.
thanks Sir its a gud defination
Thanks alot
You have given a realy nice explanation.
Very helpful explanation. Thanks.
Can u tell me the difference between sql server 2000 and sql server 2005 .
That was a very helpful explanation. This was asked to me as well. Though I answered correctly, I could not explain why I said so !!!
Thanks.
i knew the difference between these two.
can u tell me the details and difference between CUBE & ROLLUP
with regards
Ganesan.M
thanks so much…
that was a nice and understandable example..
regards,
Pradnya
thank u
Thanks for the explaination!
I really enjoy reading your articles/posts because they’re so well articulated with such simplicity that even *I* can understand it!! Thank you!
Thanks for the EXPLAINATION…It’s really helpful for me.
Pritam
Hi,
I really find your BLOG very useful and interesting.
Thanks alot
Very good explanation.
No doubt u r good mentor
Hi,
How to Retrieve Latest uplaoded data from database in sql server?
A concise explanation. Gr8….
i need difference between 2000 and 2005
Your post was really helpful. I appreciate the way it was written and presented. It becomes very easy for a layman to understand it. Thanks once again.
thanks,it is informative
will there be any difference is performance if we use HAVING in place of WHERE in a query which doesnot has any GROUPING clauses
Normally when we are looking for something on internet the
percentage of usefull info is verry less..wht we get is normally crap.But this is one of the best explanations i have came
across…thanks a lot and keep it up.
Thanks a lot
explaination is crystal clear ..
Its good explanation,but require diff between sql 2000 and 2005
good one..
good..
Thank You Sir.
Superb explaination..Good work Pinal
Hai,
The explanation is awesome..i got the exact difference now…thanks a lot..
thanks……………
it is informative :D
nice explanation.
example is also very helpful.
can u explain difference between GROUP BY and HAVING clause ?
[...] SQL SERVER – Definition, Comparison and Difference between HAVING and WHERE Clause [...]
[...] They specify a search condition for a group or an aggregate. But the difference is that HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query. (Read More Here) [...]
GROUP BY returns single occurence of a row. However I need to limit it to a specified number of occurences.
Suppose the tabe has the following:
number letter
100 A
100 A
100 A
200 B
200 B
200 B
200 B
….and I need this to limit to say 2 occurences…the output should be…
100 A
100 A
200 B
200 B
….so on for each of the multiple rows in the table..
Can you please direct me to the right query?
Dear AS,
This can be achived by various methods, however I’ve shown you only two.
For Example:
—————
You’ve a table with values like below….
Create Table #Abc (SNo Int, Letter varchar(5))
Insert Into #Abc
Select 100, ‘A’
Union All
Select 100 ,’A’
Union All
Select 100 ,’A’
Union All
Select 200, ‘B’
Union All
Select 200, ‘B’
Union All
Select 200, ‘B’
Union All
Select 200, ‘B’
and it’s output looks like this…
SNo Letter
———– ——
100 A
100 A
100 A
200 B
200 B
200 B
200 B
(7 row(s) affected)
–One Way With Common Table Expression
;With CTE
As
(
Select Row_Number() Over (Partition By Letter Order By Letter) As RowId, SNo, Letter
From #Abc )
Select SNo, Letter
From CTE
Where RowId <= 2
– Or
– Another with Cross Apply
Select a.*
From (Select Distinct SNo,Letter From #Abc) B
Cross Apply
( Select top 2 *
From #Abc x
Where B.SNo = X.SNo
And B.Letter = X.Letter ) a
–Drop Table #Abc
I hope you'll find this helpful.
Thanks,
Atif Siddyqi
Excellent. Nice explanation. Thanks a lot.
Thanks, its really helpfull
Nice explanation,This Question was asked MKCL Exam…
Nice explanation, easily understandable , thanks a lot
very nice explanation, Thanks
Thanks for such an excellent explanation.
I understand that we can use a HAVING clause without a GROUP BY clause. I tried to write a few queries using AdventureWorks database, but do not understand the purpose of using a Haveing clause without the GROUP BY clause. Will appreciate if you can explain that.
I’m wondering if there is any reason for both the WHERE and HAVING clause. Clearly WHERE filters records on fetch and/or before aggregation, and HAVING operates after aggregation. However, couldn’t the SQL compiler determine the most effecient place always. A clause based on an aggregated function (couny, sum, max, etc.), would be forced into the aggregate filter, and something without aggregated functions would be on the initial fetch. Mixing in a non-logical operator (>,100000
In this case, the clause *has* to be a having clause. That is,
select CUST,sum(REVENUE)
from order where TYPE = ‘WHOLESALE’
group by CUST
having sum(REVENUE)>100000
Incidently, thinking of this example makes me understand why non-aggregated clauses are allowed in HAVING clauses. So now, I’m thinking, using WHERE is just telling the compiler how to do something that it should be smart enough to figure out anyway.
I vote for deprecating the where clause to a hint. Any takers?
Thank You Sir
very useful for my intreview. Thank you very much.