Monday, 29 May 2017

DBMS SQL Alias ~ GNIITHELP

SQL Alias

Alias is used to give an alias name to a table or a column. This is quite useful in case of large or complex queries. Alias is mainly used for giving a short alias name for a column or a table with complex names.
Syntax of Alias for table names,
SELECT column-name 
from table-name
as alias-name
Following is an Example using Alias,
SELECT * from Employee_detail as ed;
Alias syntax for columns will be like,
SELECT 
column-name as alias-name 
from
table-name
Example using alias for columns,
SELECT customer_id as cid from Emp;

Example of Alias in SQL Query

Consider the following two tables,
The class table,
IDName
1abhi
2adam
3alex
4anu
5ashish
The class_info table,
IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
Below is the Query to fetch data from both the tables using SQL Alias,
SELECT C.id, C.Name, Ci.Address from Class as C, Class_info as Ci where C.id=Ci.id;
Result table look like,
IDNameAddress
1abhiDELHI
2adamMUMBAI
3alexCHENNAI

No comments:

Post a Comment