SQL Server- Query Processing Logic.

Use of ‘alias' for Column Name is normal and regular scenario in SQL.

But, we can observe that, one can use column alias in FROM clause and ORDER BY clause but can't use in WHERE clause.

Answer for this is “Query Processing Logic”. i.e. order in which sql clauses get executed.

For e.g.:-

        SELECT AccountName n,Amount am

         FROM  AccountTable atab

         WHERE n = 'Pravin More'

In above query , from clause is executed first followed by where clause, and at last select clause. So when

Control comes to where clause, it's trying to use a column n which doesn't exist until select completes execution and thus resulting in a “invalid column n” error.

But in below query,

        SELECT AccountName n,Amount am

         FROM  AccountTable atab

         ORDER BY n

Here Order By is execute at the end after select clause initialize column ‘n'.

 

Note:- Experienced reader may feel this Blog useless but I think for new user of SQL its important concept.

 

Thank You.

Ebook Download
View all
Learn
View all