Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am really very confused about HAVING and WHERE clause in SQL.
Can we use HAVING wherever we use WHERE??
I want to you that "What is the actual difference between WHERE and HAVING clause"???

I read a code project article about this but not really able to get the actual difference.

www.codeproject.com/Articles/25258/Where-Vs-Having-Difference-between-having-and-Wher

Somewhere i read "Having clause is used only with group functions whereas Where is not used with."

Is it really correct??
And is this the only difference??
Posted

It is quite simple: a simple selection query runs on a base set, and returns the records that match the WHERE clause. If you use aggregation, you derive a new result set, that can be also filtered. For filtering based on the result of the aggregation itself you can use HAVING.
 
Share this answer
 
This blog post Difference between WHERE vs HAVING clause in SQL - GROUP BY Comparison with Example[^] gives a good explanation of the difference.

WHERE is used for filtering the individual records and HAVING to filter the result off an aggregate function[^] on the used GROUP BY.
 
Share this answer
 
Comments
Maciej Los 17-Nov-13 17:35pm    
short and to the point!
My 5!
SQL
You can use Having Clause with the GROUP BY function in query and WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900