Sql - Order of Statements

The below is the correct answer to one of the questions in the lesson at the link below. Im struggling with the order of the statements. Why the order of GROUP BY, HAVING and ORDER BY need to be in the below order and not for example- GROUP BY, ORDER BY and then HAVING. I understand we need to group the counts first. But it seems like HAVING and ORDER BY should work in either order. Any advice is appreciated!

SELECT category, COUNT()
FROM startups
GROUP BY category
HAVING count(
) > 3
ORDER BY count(*) desc

Aggregate Functions | Codecademy

ORDER BY is last when you’re writing a query.
Unless you’re limiting your results, then it’s LIMIT.

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT

For example, see:
https://learnsql.com/blog/sql-order-of-operations/

2 Likes