Column aliases are used mainly to shorten code and make column names more readable.
Code becomes shorter as long table names and unnecessary identification of columns (e.g., there may be 2 IDs in the table, but only one is used in the statement) can be avoided. Along with table aliases this allows you to use longer descriptive names in your database structure while keeping queries upon that structure concise.
Furthermore they are sometimes required, for instance in views, in order to name computed outputs.
Aliases can be created in all versions of SQL using double quotes ("
).
SELECT
FName AS "First Name",
MName AS "Middle Name",
LName AS "Last Name"
FROM Employees
You can use single quotes (\\'
), double quotes ("
) and square brackets ([]
) to create an alias in Microsoft SQL Server.
SELECT
FName AS "First Name",
MName AS 'Middle Name',
LName AS [Last Name]
FROM Employees
Both will result in:
First Name | Middle Name | Last Name ––––– |––––– | ———–
James | John | Smith
John | James | Johnson
Michael | Marcus | Williams
This statement will return FName
and LName
columns with a given name (an alias). This is achieved using the AS
operator followed by the alias, or simply writing alias directly after the column name. This means that the following query has the same outcome as the above.
SELECT
FName "First Name",
MName "Middle Name",
LName "Last Name"
FROM Employees
First Name | Middle Name | Last Name ––––– |––––– | ———–
James | John | Smith
John | James | Johnson
Michael | Marcus | Williams