At first I was going to say it’s because you’re using a condition on an aggregate. But, I think it’s b/c the HAVING clause is executed before the SELECT clause in the order of operations (behind the scenes).
I think that particular expression, 2 > 500 is simply going to evaluate to a boolean result; I don’t know if that’s the case in every sql flavour but it’s always best to avoid ambiguity. In short I don’t think you can use column numbers when you’re using operators that can act on numbers.
If you’ve got a rather complicated expression in SELECT then aliasing the column might be a nice option, for example-
SELECT
location,
ROUND(AVG(employees), 2) AS average_size
FROM startups
GROUP BY location
HAVING average_size > 500;
HAVING is a condition/a grouped query (b/c it filters groups not rows) that uses an aggregate.
When you have something like HAVING 2 > 500 won’t work b/c it is executed before the SELECT statement.
In Postgres for example:
" Since the HAVING clause is evaluated before the SELECT clause, you cannot use aliases in the HAVING clause. Because at the time of evaluating the HAVING clause, the column aliases specified in the SELECT clause are not available."
That can vary based on which SQL you’re using, in SQLite aliases in HAVING are perfectly valid and it would work in this lesson. Then again SQLite does a few funny things so you might be safer avoiding that option just in case you have to switch between flavours.