What are differences between ‘WHERE’ clause and ‘HAVING’ clause in SQL?


Warning: file_get_contents(/home/duyingl1/public_html/researchblog.org/wp-content/plugins/terms-and-conditions/terms-and-conditions.txt) [function.file-get-contents]: failed to open stream: Permission denied in /home/duyingl1/public_html/researchblog.org/wp-content/mu-plugins/terms-and-conditions.php on line 74

Warning: file_get_contents(/home/duyingl1/public_html/researchblog.org/wp-content/plugins/terms-and-conditions/privacy-policy.txt) [function.file-get-contents]: failed to open stream: Permission denied in /home/duyingl1/public_html/researchblog.org/wp-content/mu-plugins/terms-and-conditions.php on line 78

Having clause is usually used with Group By clause although it can be used without it too. Having is just an additional filter to Where clause.

Where clause applies to the individual rows whereas Having clause is used to test some condition on the group(usually aggregate methods) rather than on individual rows.

Example of HAVING and WHERE in one query:

SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = ‘CA’
GROUP BY titles.pub_id
HAVING AVG(titles.price) > 10

Thanks to S.Vinothkumar.

~ by yingliang on June 30, 2009.

Leave a Reply




*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image