RoiEX RoiEX - 4 months ago 64
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.)?


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


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