Monday, February 11, 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
WHERE E.Address='Bangalore'

Here :
E 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.

--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog