Last answered:

18 Apr 2022

Posted on:

11 Apr 2022

0

Resolved: Why not use names_count as part of HAVING condition?

In the SELECT statement, COUNT(first_name) is aliased as names_count.  Why write the HAVING statement as COUNT(first_name) < 200, instead of HAVING names_count < 200?
Why would that not work?
THank you!

1 answers ( 1 marked as helpful)
Instructor
Posted on:

18 Apr 2022

0

Hi Catherine!
Thanks for reaching out.

I understand your confusion. The actual execution of MySQL statements is a bit tricky. However, the standard does specify the order of interpretation of elements in the query. This is basically in the order that you specify, although I think

FROM clause

WHERE clause

SELECT clause

GROUP BY clause

HAVING clause

ORDER BY clause

This is important for understanding how queries are parsed. You cannot use a column alias defined in a SELECT in the WHERE clause, for instance, because the WHERE is parsed before the SELECT. On the other hand, such an alias can be in the ORDER BY clause.

Hope this helps.
Best,
Tsvetelin

Submit an answer