Where Clause And Having Clause In Sql
What is the difference between "where" clause and a "having" clause?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Sep 25, 2012, 8:52 PM EDT
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
Also The difference is when the clause is applied. WHERE is applied much earlier in the creation of the result set and I believe can take advantage of indexes and other optimizations. HAVING is applied much later.
HAVING is usually much slower than using WHERE as the WHERE clause usually is more restrictive earlier on. However, HAVING is very useful when wanting to filter on aggregated or aliased columns as these are created after the application of the WHERE clause.
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 25, 2012, 8:10 PM EDT