Where vs Having SQL

Question

What is the difference between WHERE and HAVING clauses.

Answer

The WHERE clause restricts which rows are returned while the HAVING clause operates on groups of rows. HAVING is always used with GROUP BY and must have a function it restricts. The rule says if a condition refers to an aggregate function put that condition in the HAVING clause otherwise use the WHERE clause. Another rule says you can not use HAVING clause unless you also use GROUP BY.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *