x

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 '10 at 09:36 AM in Default

OracleApprender gravatar image

OracleApprender
771 68 73 75

(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 '10 at 12:46 PM

goyuix gravatar image

goyuix
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 '11 at 03:40 AM

jatin gravatar image

jatin
1

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x375
x361
x235
x2

asked: Jan 18 '10 at 09:36 AM

Seen: 4883 times

Last Updated: Jan 22 '10 at 06:35 AM