Skip to content
Advertisement

Why can’t I use column aliases in the next SELECT expression?

Can I modify the next to use the column aliases avg_time and cnt in an expression ROUND(avg_time * cnt, 2)?

It raises the next error:

The next, however, works fine (use primary expressions instead of column aliases:

Advertisement

Answer

You can use a previously created alias in the GROUP BY or HAVING statement but not in a SELECT or WHERE statement. This is because the program processes all of the SELECT statement at the same time and doesn’t know the alias’ value yet.

The solution is to encapsulate the query in a subquery and then the alias is available outside.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement