Why do we use WHERE instead of HAVING in this example?

In the context of this code challenge from the ‘Performing SQL Calculations’ submodule within the ‘SQL for Back-end Development’ module (Code Challenge 7, I am a bit confused as to why we are using the WHERE clause instead of the HAVING clause.

I understand that WHERE is used for filtering rows and HAVING is used for filtering groups, but I would really appreciate an explanation of this particular example.

You answered your own question? I think what helps, is this:

where is evaluated before group by, while having is evaluated after group by

so where first only filters out the records you want to before grouping. In a way, this is an optimization: you don’t want to group data you don’t need

6 Likes

Thank you very much for the answer! I was particularly looking for what you said in the last phrase. Now it makes sense.

2 Likes