x
login about faq Site discussion (meta-askssc)

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
763 53 66 73

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

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x359
x321
x210
x2

asked: Jan 18 '10 at 09:36 AM

Seen: 2827 times

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

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.