• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL Having Clause

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was just working on SQL Having clause. I wrote the following query using having clause:


Then I wrote the following query using where condition and not using the having clause:


Both are basically giving the same result. I am wondering now the usage of having clause because if it's usage is to filter the records that a GROUP BY returns, then it can be done using the where condition too right?

Thanks.
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A HAVING clause restricts the results of a 'GROUP BY'.
The HAVING clause is applied to each group,A WHERE clause is applied to a select list.
Having clause can also filter your data on aggregate functions like, sum, avg, min etc.




Just try filter your query on aggregate function, then you will come to know about difference. for example try to filter on SUM(PRICE) >0

Shailesh
[ November 11, 2008: Message edited by: Shailesh Chandra ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shailesh Chandra:



Just try filter your query on aggregate function, then you will come to know about difference. for example try to filter on SUM(PRICE) >0

Shailesh

[ November 11, 2008: Message edited by: Shailesh Chandra ]



Yes Shailesh, where clause does not seem to work on aggregate functions. Where as Having clause works perfectly fine.

Thanks.
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic