What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?

by Gourav| Views: 931

What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?

Answers (1)
 
jack Said..

Difference between a HAVING CLAUSE and a WHERE CLAUSE:


  • WHERE and HAVING clauses specify a search condition for a group or an aggregate. Basic difference is that HAVING can be used only with the SELECT statement.


  • HAVING specifies a search condition for a group or an aggregate function used in SELECT statement & it used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.


  • A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows.


  • Having Clause is basically used with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query.


  • WHERE is applied much earlier in the creation of the result set and it can take advantage of indexes and other optimizations. HAVING is applied much later so its much slower than using WHERE.


  • However, HAVING is very useful when wanting to filter on aggregated or aliased columns as these are created after the application of the WHERE clause.



Register or Login to Post Your Opinion/Answer