I am trying to understand the difference between HAVING and WHERE. I understand that HAVING
is used with GROUP BY statements. However, I cannot understand why the following statement is accepted:
select SUM(child_id) from children WHERE child_ID = 5 GROUP BY Child_ID
Shouldn't the correct statement be select SUM(child_id) from children GROUP BY Child_ID HAVING child_ID = 5
?
WHERE clauses are executed before the grouping process has occurred, and only have access to fields in the input table. HAVING is performed after the grouping pocess occurs, and can filter results based on the value of aggregate values computed in the grouping process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With