ALIAS NAMES

It is a mechanism in SQL Server which allows users to provide alternative or another name of the Entities (Tables) and their Attributes (columns). These Alias names are mainly used to hide the original name of the table or column. These are always associated with SELECT statement.

Note: Alias names are only for temporary displaying purpose. Those names not stored permanently into the database.

Example:

SELECT EMPNO AS ENO,ENAME AS EMPLOYNAME,SAL AS BASICSAL,DEPTNO AS DNO FROM EMP

Note: While providing alias names for the columns we have use a Keyword called 'AS'
in between original column name and alias column name.

The above statement simply displays employ details by providing temporary column names.

Alias names are also used to provide alternative names for the newly derived values.

Example: Write a query to select SAL as BASIC_SAL,DA,HRA,PF,and NET,GROSS by taking the following specification.

30% of salary as DA
20% of salary as HRA
10% of salary as PF
SAL+DA+HRA as GROSS
GROSS-PF as NET

SELECT SAL AS BASIC_SAL,

SAL*30/100 AS DA,
SAL*20/100 AS HRA,
SAL*10/100 AS PF,
(SAL+SAL*30/100+SAL*20/100) AS GROSS
(SAL+SAL*30/100+SAL*20/100) - (SAL*10/100) AS NET
FROM EMP

In the above query the DA,HRA,PF,GROSS,NET are alias names provided for the newly derived values.
Share/Bookmark

No comments: