WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL Aliases


SQL aliases are used to temporarily rename a table or a column heading.


SQL Aliases

SQL aliases are used to give a database table, or a column in a table, a temporary name.

Basically aliases are created to make column names more readable.

SQL Alias Syntax for Columns

SELECT column_name AS alias_name
FROM table_name;

SQL Alias Syntax for Tables

SELECT column_name(s)
FROM table_name AS alias_name;

Database

In this tutorial we will use the sample database.

Below is a selection from the "Customers" table:

CustomerID PersonName Phone Email adderss User Pass
2 Barbara W. Messerly 405-289-4169 RobertCBray@jourrapide.com Brishemed ou6eeseeRei
3 Billy P. Losada 804-567-0813 BillyPLosada@armyspy.com Dited1976 evahNgaiz1
4 Edna M. Hymel 618-966-9060 EdnaMHymel@armyspy.com Procke Oojo7is4ei

And a selection from the "Job" table:

JobID CustomerID personID Dateof birth workerID
501 15 2 1982-10-16 3
502 10 5 1992-12-19 1
503 16 7 1986-03-11 2

Alias Example for Table Columns

The following SQL statement specifies two aliases, one for the CustomerName column and one for the ContactName column.

Example

SELECT CustomerName AS Customer, PersonName AS [Phone]
FROM Customers;

In the following SQL statement we combine four columns (Email Adderss, User, Pass) and create an alias named "Address":

Example

SELECT CustomerName, Address+', '+Email Adderss+', '+User+', '+Pass AS Address
FROM Customers;
SELECT CustomerName, CONCAT(Address,', ',Email Adderssy,', ',User,', ',Pass) AS Address
FROM Customers;

Alias Example for Tables

The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the Horn). We use the "Customers" and "Orders" tables, and give them the table aliases of "c" and "o" respectively (Here we have used aliases to make the SQL shorter):

Example

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;

Aliases can be useful when:

  • There are more than one table involved in a query
  • Functions are used in the query
  • Column names are big or not very readable
  • Two or more columns are combined together