Labels

Wednesday, 23 December 2015

SQL Server - Alias Names

Alias Names 

Alias is a duplicate name or an alternate name for the original base table or for the original expression
using alias names we can generate meaningful reports to the end-users
It is classified into 3 types

                                * Column level alias
                                * Expression level alias
                                * Table level alias

1. Column level alias

                Providing the alias names for the columns in the table is known as column level alias .

Syntax

             Select CN1 as "Column-name1",CN2 as "column-name2" from <T.N>

Note 
           In the above example the keyword 'as' is optional and also " " is optional
If alias name contains more than oneword then only we have to use " " or [ ]

2. Expression level alias

                Providing the alias name for the expression

select empid,sal,sal*12 annualsalary from emp;

Is it possible to apply an expression on alias column?

No , Not possible

Ex

     Select empid,sal salary,sal*12 annualsalary from emp where annualsalry>50000

Note

        In the above example we will get errormessage saying that invalid columname 'annualsalary'

To avoid the above problem use expression directly


select eid employeeid,sal salary,sal*12 from emp where sal*12>50000

No comments:

Post a Comment