SQL aliases provide temporary names to tables or columns within a query.
Aliases are frequently utilized to enhance the readability of column names.
An alias is valid only for the duration of the query in which it is defined.
Aliases are defined using the AS keyword.
SELECT CustomerID AS ID FROM Customers; |
In many database languages, you can omit the AS keyword and achieve the same outcome.
SELECT CustomerID ID FROM Customers; |
When an alias is applied to a column:
SELECT column_name AS alias_name |
When an alias is assigned to a table:
SELECT column_name(s) |
Here is a portion of the Customers and Orders tables utilized in the examples:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
OrderID |
CustomerID |
EmployeeID |
OrderDate |
ShipperID |
10248 |
90 |
5 |
7/4/1996 |
3 |
10249 |
81 |
6 |
7/5/1996 |
1 |
10250 |
34 |
4 |
7/8/1996 |
2 |
The following SQL statement establishes two aliases, one for the CustomerID column and another for the CustomerName column:
SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers; |
If you intend for your alias to include one or more spaces, such as “My Great Products“, enclose your alias with square brackets or double quotes.
Utilizing [square brackets] for aliases containing space characters:
SELECT ProductName AS [My Great Products] FROM Products; |
Employing “double quotes” for aliases containing space characters:
SELECT ProductName AS “My Great Products” FROM Products; |
Note: Some database systems allows both [] and “”, and some only allows one of them. |
The following SQL statement creates an alias named “Address” by combining four columns (Address, PostalCode, City, and Country):
SELECT CustomerName, Address + ‘, ‘ + PostalCode + ‘ ‘ + City + ‘, ‘ + Country AS Address FROM Customers; |
Note: To make the SQL statement above function in MySQL, utilize the following adjustment:
SELECT CustomerName, (Address || ‘, ‘ || PostalCode || ‘ ‘ || City || ‘, ‘ || Country) AS Address FROM Customers; |
The same principles apply when you wish to assign an alias to a table.
Refer to the Customers table as Persons instead.
SELECT * FROM Customers AS Persons; |
Although employing aliases on tables may seem unnecessary, it can significantly abbreviate SQLstatements, particularly when multiple tables are involved in queries.
The subsequent SQL statement retrieves all orders from the customer with CustomerID=4 (Around the Horn). We utilize the “Customers” and “Orders” tables, assigning them the table aliases of “c” and “o” respectively. In this case, aliases are utilized to condense the SQL query.
SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName=‘Around the Horn’ AND c.CustomerID=o.CustomerID; |
The following SQL statement achieves the same outcome as the previous one, but without using aliases:
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName |
Aliases prove beneficial in the following scenarios: