A query will fail when a column is aliased and then referenced in the WHERE clause.
SELECT col1, col2 AS c2 FROM tbl1 WHERE c2 BETWEEN 2 AND 6 |
This query will throw the error:
Msg 207, Level 16, State 1, Line 3 Invalid column name 'c2'.
Attempting to reference the aliased column in the WHERE clause does not work because of the logical query processing taking place. The WHERE is evaluated before the SELECT clause. Therefore, the column c2 does not exist when WHERE is evaluated.
The correct way to reference the column in this example would be:
SELECT col1, col2 AS c2 FROM tbl1 WHERE col2 BETWEEN 2 AND 6 |