Code has been added to clipboard!

SQL AS – the Alias Statement

Reading time 2 min
Published Aug 9, 2017
Updated Oct 15, 2019

SQL AS renames a column or table to a more convenient alias (a correlation name) without changing the original names in the database. This makes writing queries easier when the original table or column names are long or complicated.

SQL AS: Main Tips

  • SQL AS is used as a temporary name for the purpose of a particular SQL query.
  • Aliases ensure better readability.

SQL AS for Columns

Example
SELECT col_name AS al_name
FROM tbl_name;

The example below describes both CustomerName and ContactName aliases.

When there are spaces between names of the columns, you must use square brackets or quotation marks:

Example
SELECT ID as CustomerID, Name AS Customers
FROM Customers;
Theory is great, but we recommend digging deeper!

SQL AS for Tables

Example
SELECT col_name(s)
FROM tbl_name AS al_name;

The example below picks out every order from customer with CustomerID=4. The aliases of the tables are given o and c for the orders and the customers table. This is done to make the SQL code shorter.

Example
SELECT o.ID, c.Name
FROM Customers AS c, Customer_orders AS o
WHERE c.id = 2 AND c.ID = o.customer_id;

And a matching example with no alias:

Example
SELECT Customer_orders.ID, Customers.Name
FROM Customers, Customer_orders
WHERE Customers.id = 2 AND Customers.ID = Customer_orders.customer_id;

Demo Database

In the table below you can see a piece of Customers table:

ID Name Contact Address City Postal_Code Country
1 Ben Choplinks Ben Choplink Obeesre Str. 51 Rome 11207 Italy
2 Donald Rich Donald Richario Avda. de la Confgfstitución 4122 Tallin 17021 Estonia
3 Lilly Smilkins Lilly Smilkin Matadsderos 2312 Eguero 14023 Mexico
4 Brandinina Tom Hitchins 110 Hanegover Sq. London WB2 2DP UK
5 Carizmos Christiano Kerrys Berguvsesvägen 9 Luleå S-968 43 Sweden

Customer_Orders

id customer_id employee_id
20408 2 7
85471 37 3
75864 77 8

SQL AS: Summary

  • Using alias does not change a column or table name in the original database.
  • Aliases make complicated or long names easier read.