The Alias keyword is used in SQL to give an alternative name for a column or table. This will not change the column or table name in the database.
SQL Syntax for Tables
SELECT column_name1, column_name2 ...
FROM table_name AS alias_name
SQL Syntax for Column
SELECT column_name1 AS alias_name1, column_name2 AS alias_name2 ...
FROM table_name
The Select Statement Example
Below is shown two tables, the first being the "Customers" table:
C001 |
Fredrick Johnson |
FJohnson@yahoo.com |
Berkeley Gardens 12 Brewery |
London |
WX1 6LT |
UK |
C002 |
Mary Davidson |
Mary@gmail.com |
35 King George |
Madrid |
28023 |
Spain |
C003 |
George Clooney |
George@hotmail.com |
Obere Str. 57 |
Berlin |
12209 |
Germany |
C004 |
David Louis |
David@mail.com |
2743 Bering St. |
NewJersy |
07019 |
USA |
C005 |
Johnny Anderson |
Johnny@gmail.com |
55 Grizzly Peak Rd. |
NewJersy |
07019 |
USA |
C006 |
Paul Henriot |
PaulHenriot@yahoo.com |
59 rue de l'Abbaye |
Paris |
51100 |
France |
C007 |
Henry King |
Henry@mail.com |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
C008 |
Robert John |
Robert@gmail.com |
87 Polk St. Suite 5 |
San Francisco |
94117 |
USA |
Second table is "Orders" table:
D001 |
C001 |
4/3/2007 12:00:00 AM |
150.23 |
D002 |
C003 |
1/22/2005 12:00:00 AM |
123.57 |
D003 |
C005 |
3/19/2003 12:00:00 AM |
220.43 |
D004 |
C007 |
7/21/2008 12:00:00 AM |
157.72 |
D005 |
C008 |
2/28/2009 12:00:00 AM |
223.78 |
D006 |
C011 |
3/21/2005 12:00:00 AM |
245.34 |
Text Editor below has the SQL Query, click the "View Result" button, to see the result.
Shown below in the SQL usage of Alias.
Select C.CustomerID, C.CustomerName, C.City, O.DATE, O.Amount
From Customers C, Orders O
where C.CustomerID = O.CustomerID;
Shown below is the SQL that gives the same output without Alias.
Select Customers.CustomerID, Customers.CustomerName, Customers.City,
Orders.DATE, Orders.Amount
From Customers, Orders
where Customers.CustomerID = Orders.CustomerID;
SQL in Text Editor: