Brenno,
Achei alguns sites explicando isso:
===============================================================
Qual é a sequência de execução da cláusula Group By, Having e Where no SQL Server?
em ordem:
- FROM & JOIN s determinam e filtram as linhas
- WHERE mais filtros nas linhas
- GROUP BY combina essas linhas em grupos
- HAVING filtros grupos
- ORDER BY organiza as linhas / grupos restantes
- LIMIT filtros nas linhas / grupos restantes
Aqui está a sequência completa para o servidor sql:
- FROM
- ON
- JOIN
- WHERE
- GROUP BY
- WITH CUBE or WITH ROLLUP
- HAVING
- SELECT
- DISTINCT
- ORDER BY
- TOP
https://qastack.com.br/programming/1130062/what-is-the-execution-sequence-of-group-by-having-and-where-clause-in-sql-serve
===============================================================
How to use" HAVING "and "ORDER BY" clause in sql
Asked 12 years, 5 months ago - Modified 12 years, 5 months ago
Use where here instead of having.
having is useful for narrowing conditions on aggregate values.
where is useful for narrowing conditions on un-aggregated data.
https://stackoverflow.com/questions/3608150/how-to-use-having-and-order-by-clause-in-sql
===============================================================
How to use" HAVING "and "ORDER BY" clause in sql
https://stackoverflow.com/questions/3608150/how-to-use-having-and-order-by-clause-in-sql
===============================================================
Execution sequence of Group By, Having and Where clause in SQL Server?
Asked 13 years, 7 months ago - Modified 5 months ago
https://stackoverflow.com/questions/1130062/execution-sequence-of-group-by-having-and-where-clause-in-sql-server
===============================================================
[]'s,
Fabio I.