web analytics

Can't refer to column aliases in the WHERE clause

Options

codeling 1595 - 6639
@2019-10-08 11:48:22

If you refer column alias in the where caluse, you will get  the error:

SELECT YEAR(transaction_date) AS YEAR,amount FROM dev.sale

WHERE YEAR=2019

You get the error

Invalid column name ‘year’.

It is because the column alias are not immediately known to the WHERE clause.

 

@2019-10-08 11:49:37

select * from 

(

SELECT YEAR(transaction_date) AS YEAR,amount FROM dev.sale

)

WHERE YEAR=2019

@2019-10-08 11:51:23

But you can use column alias in the ORDER BY clause because ORDER BY is executed lastly after the entire column list is known.

SELECT YEAR(transaction_date) AS YEAR,amount FROM dev.sale

ORDER BY YEAR

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com