RoiEX RoiEX - 28 days ago 20
SQL Question

Calculating with aliases

Simple question:

Can I use an alias to calculate with? Is

SELECT example1 / example2 AS ratio
, SQRT(ratio) AS squareroot
FROM example_table


legal and are there special rules to it (defining alias before using it etc.)?

Answer

No, you should define an alias before using it, so you either:

select example1 / example2 as ratio
     , sqrt(example1 / example2) as squareroot 
from example_table

or

select ratio
     , sqrt(ratio) as squareroot
from ( 
  select example1 / example2 as ratio 
  from example_table
) as tbl
Comments