wood burning stoves 2.0*
The moose likes JDBC and the fly likes SQL Having Clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL Having Clause" Watch "SQL Having Clause" New topic
Author

SQL Having Clause

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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.


Be Humble... Be Nice.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081


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 ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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.
 
Consider Paul's rocket mass heater.
 
subject: SQL Having Clause
 
Similar Threads
Derby TOP Clause
count(*) vs count(id)
Search with 4 params
How to do groupby in java
MySQL update issue