HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

SQL Aliases

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

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

Aliases can be useful when:


Example

              SELECT Employee_id AS id
FROM employee;
aliases ex

AS is Optional


In most database languages, you can skip the AS keyword and get the same result:

Example

              SELECT Employee_id id
FROM employee;
aliases ex
Syntax:

When alias is used on column:

SELECT column_name AS alias_name
FROM table_name;

When alias is used on table:

SELECT column_name(s)
FROM table_name AS alias_name;

Demo Employee table


This employee table is used for examples:

demo table

Demo Products table


This product table is used in examples:

demo products table

Alias for Columns


The following SQL statement creates two aliases, one for the Employee_id column and one for the Employee_name column:

Example

              SELECT  Employee_id  AS id , Employee_name  AS Employee
FROM employee;

aliases 2 ex

Using Aliases With a Space Character


If you want your alias to contain one or more spaces, like "My Good Products" , surround your alias with square brackets or double quotes.

Example:

Using [square brackets] for aliases with space characters:

SELECT Product_name AS [My Good Products]
FROM products;

Example

              

Using "double quotes" for aliases with space characters:

SELECT Product_name AS "My Good Products"
FROM products;

aliases with space character


Note:Some database systems allows both [] and "", and some only allows one of them.

Concatenate Columns


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

Example

              

This statement is work in MYSQL.

SELECT Employee_name ,CONCAT(Address, ',' , Pincode , ',' , City, ',' , Country) AS Address
FROM employee;

aliases with concat

Alias for Tables


Example

              

Refer to the Customers table as Persons instead:

SELECT * FROM employee AS Persons;

aliases with table

It can make the SQL statements shorter when you are using more than one table in your queries.

The following SQL statement selects all the orders from the employee with Employee_id=5 (Horst Kloss). We use the "employee" and "products" tables, and give them the table aliases of "e" and "p" respectively :

Example

              

SELECT p. Order_id , p. Order_date , e. Employee_name
FROM employee AS e, products AS p
WHERE e. Employee_name = 'Horst Kloss' AND e. Employee_id = p. Employee_id;

aliases with table

The following SQL statement is the same as above, but without aliases:

Example

 

SELECT products . Order_id , products. Order_date , employee. Employee_name
FROM employee , products
WHERE employee . Employee_name = 'Horst Kloss' AND employee . Employee_id = products. Employee_id;

You can click on above box to edit the code and run again.

Output

aliases with table