This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes A simple SQL question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "A simple SQL question" Watch "A simple SQL question" New topic
Author

A simple SQL question

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a very uneducated question regarding SQL. What is the difference between HAVING and WHERE?
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cameron Park:
Hi, I have a very uneducated question regarding SQL. What is the difference between HAVING and WHERE?

The WHERE clause is applied [/I]before[/I] grouping occurs, the HAVING clause after.
Consider
SELECT dept, count(*)
FROM emp
WHERE salary > 100000
GROUP BY dept
This will take the employees whose salary exceeds 100000, and count the number of such employees grouped on a per department basis. Note that the salary condition is applied before any grouping occurs. Contrast this with
SELECT dept, count(*)
FROM emp
GROUP BY dept
HAVING AVG(salary) > 100000
This will first count employees, grouped per department, and from this list show you only those departments where the average salary exceeds 100000. The condition is applied to the groups themselves, not to the records used in constructing the groups.
- Peter
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Thank you Peter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple SQL question