Monday, 11 February 2013

SQL Table Alias and Column Alias Syntax and Example

SQL Table and Column Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.An alias name could be anything, but usually it is short.

SQL Alias Syntax for Tables

SELECT column_name(s) FROM Table_name Alias_name

SQL Alias Syntax for Columns

SELECT column_name AS alias_name FROM  Table_name

Example :
Suppose we have a EMP Table as follows :

E_Id
First_Name
Last_Name
Dept_Id
Address
Salary
1001
Ravi
Kumar
1
Hyderabad
20000
1002
David
Smith
2
Bangalore
35000
1003
Victory
Venkatesh
1
Hyderabad
50000
1004
Tamatam
Reddy
3
Bangalore
25000
1005
William
Smith
2
Hyderabad
40000
1006
King
Fisher
6
Bangalore
30000

Now see the following Query with Alias Names for Columns and Table :

Select E.E_Id,E.First_Name as F_Name, E.Last_Name as L_Name from EMP E WHERE E.Address='Bangalore'

Here :
is an Alias Table name for EMP
F_Name is an Alias column name for First_Name
L_Name is an Alias column name for Last_Name

Out Put :

E_Id
F_Name
L_Name
1002
David
Smith
1004
Tamatam
Reddy
1006
King
Fisher


Aliases can make queries easier to write and to read.

Thanks

TPREDDY Alias  as TPR

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts