What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?

Can anybody explain to me the differences between Having and Where clauses ? And also can we use both within the same query ? Please give an example if possible. Thank you.

more ▼

asked Jan 18, 2010 at 09:36 AM in Default

avatar image

771 73 75 79

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

While they accomplish a very similar function, their uses are entirely different:

First, a HAVING clause is only allowed / makes sense with a GROUP BY - which would typically imply you are using an aggregate of some sort in your SELECT statement. This also stems from the fact that in many cases aggregate functions are not allowed in the WHERE expression.

Second, the following is the order in which key expressions of your statement are executed:

  • FROM clause
  • WHERE clause
  • GROUP BY clause
  • HAVING clause
  • SELECT clause
  • ORDER BY clause

Allowing the data to first be refined by the FROM and the WHERE expressions allows for certain performance improvements when using the HAVING clause on a (potentially) smaller data set.

more ▼

answered Jan 18, 2010 at 12:46 PM

avatar image

1 1 2 1

(comments are locked)
10|1200 characters needed characters left

this is very good explanation, for easy and better explanation you can visit this link http://dotnetpeoples.blogspot.com/2011/04/having-clause-vs-where-clause.html

more ▼

answered Apr 21, 2011 at 03:40 AM

avatar image


(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Jan 18, 2010 at 09:36 AM

Seen: 6310 times

Last Updated: Jan 22, 2010 at 06:35 AM

Copyright 2018 Redgate Software. Privacy Policy