Monday, October 27, 2014

MySQL WHERE vs. HAVING

With a SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... query, MySQL first selects rows from the table(s) by evaluating the WHERE conditions. These rows are then grouped by GROUP BY clause and further filtered by the HAVING clause.

Conditions in WHERE clause are applied for SELECT, before any GROUP BY operations.

HAVING conditions, on the other hand, are applied for GROUP BY operations, after SELECT is completed, therefore can have aggregate functions to be evaluated in the conditions.


No comments:

Post a Comment