Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to comprehend why a WHERE clause is being accepted [duplicate]

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 ?

like image 453
Dot NET Avatar asked Feb 18 '23 05:02

Dot NET


1 Answers

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.

like image 136
Pieter Geerkens Avatar answered May 03 '23 01:05

Pieter Geerkens