SQL Aliases
You can give an alias to any column or any table in SQL by using the AS statement. This temporary name can be used in the output of a query to enhance the clarity and avoid ambiguity.
Syntax for column:
SELECT column_name1 AS alias1, column_name2 AS alias2,....
FROM table_name;
Syntax for table:
SELECT column_name1, column_name2,....
FROM table_name AS alias1;
Consider the table,
Emp_Add given below:
| ssn |
fname |
lname |
address |
city |
state |
| 512687458 |
Joe |
Smith |
83 First Street |
Howard |
Ohio |
| 758420012 |
Mary |
Scott |
842 Vine Ave. |
Losantiville |
Ohio |
| 102254896 |
Sam |
Jones |
33 Elm St. |
Paris |
New York |
| 876512563 |
Sarah |
Ackerman |
440 U.S. 110 |
Upton |
Michigan |
Suppose, we want to display
fname as
FirstName and
lname as
LastName in the output. The query for the same will be:
SELECT fname AS FirstName, lname AS LastName
FROM Emp_Add;
The output will be:
| FirstName |
LastName |
| Joe |
Smith |
| Mary |
Scott |
| Sam |
Jones |
| Sarah |
Ackerman |
The name of our table is
Emp_Add, which is quite big. We might want to shorten it for our convenience. Suppose, we want to use the name,
emp instead of Emp_Add. The SQL query for the same would be:
SELECT emp.fname AS FirstName, emp.lname AS LastName
FROM Emp_Add AS emp;
In the above query, we have used the shortened name
emp instead of it's original name. The output will be the same as the above.