English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Usage and examples of SQL AS keyword

SQL Keywords Reference

AS

AS command is used to rename columns or tables using aliases.

Aliases exist only during the query.

Column alias

The following SQL statement creates two aliases, one for the CustomerID column and another for the CustomerName column:

 SELECT CustomerID AS ID, CustomerName AS Customer
 FROM Customers;

The following SQL statement creates two aliases. Please note that if the alias name contains spaces, it must be enclosed in double quotes or brackets:

 SELECT CustomerName AS Customer, ContactName AS [Contact Person]
 FROM Customers;

The following SQL statement creates an alias named 'Address' that combines four columns (Address, PostalCode, City, and Country):

  SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country 
  AS Address
 FROM Customers;

Note:To make the above SQL statement run in MySQL, please use the following command:

SELECT CustomerName, CONCAT(Address, ', ', PostalCode, ', ', City, ', ', Country) AS Address
FROM Customers;

Table Aliases

The following SQL statement selects CustomerID = 4All customer orders. We use the "Customers" and "Orders" tables and assign table aliases "c" and "o" to them (using aliases makes SQL shorter here):

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;

SQL Keywords Reference