Monday, April 23, 2012

Difference between where and having clause


1. The WHERE clause specifies the criteria which individual records must meet to be selcted by a query. It can be used without the GROUP BY clause. The HAVING clause cannot be used without the GROUP BY clause.
2. The WHERE clause selects rows before grouping. The HAVING clause selects rows after grouping.
3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain aggregate functions.
 The HAVING clause allows you to filter the results of aggregate functions,
such as COUNT() or AVG() or SUM(), or MAX() or MIN(), just to name a few.
HAVING provides you a means to filter these results in the same query,
as opposed to saving the results of a WHERE clause SQL statement to a temporary table
and running another query on the temporary table results to extract the same results.