;

SQL ALIAS


In this tutorial, we will learn what is an alias and how to use ALIAS in SQL.

SQL ALIAS

  • In SQL, An Alias is a Temporarily assign another name to a table or a column for the duration of the SELECT  statement.
  • When You assigning an Alias to a table or column does not actually rename the column name or table name.
  • Alias is often used to make column names or table name more readable.
Syntax - Alias syntax for a column name
SELECT column AS AliasName
FROM tableName;
Syntax - Alias syntax for a table name
SELECT column1,column2,... columnN
FROM tableName as AliasName;

Example:

Let us consider this table "Employee" for records.

Table Name: Employee

ID EmpName FlatNo City Country Gender Salary
1 Shankar A - 255 Delhi India male 25000
2 Sourabh A - 3 Delhi India male 30000
3 Ranvijay B - 526 Mumbai India male 15000
4 Kapil F - 255 Noida India male 25000
5 Shalini E - 205 Jaipur India female 18000
6 Rakesh J - 295 Faridabad India male 23000
7 Akshay A - 2055 Mumbai India male 21000
8 Sarah A - 55 New York US female 76000
9 Rocky A - 2505 Noida India male 28000

Example of Alias for ColumnName

For creating Alias of the single or multiple columns, a query will be:

Example - Alias for a column name
SELECT ID AS EmpID, EmpName AS Name
FROM Employee;
Output
ID Name
1 Shankar
2 Sourabh
3 Ranvijay
4 Kapil
5 Shalini
6 Rakesh
7 Akshay
8 Sarah
9 Rocky

For creating Alias of the single or multiple columns, and alias contains space. Then you use a square bracket or double quotation marks, a query will be:

Example - Alias using square bracket 
SELECT ID, EmpName AS [Employee Name]
FROM Employee;
Example - using quotation marks
SELECT ID, EmpName AS 'Employee Name'
FROM Employee;
Output
ID Name
1 Shankar
2 Sourabh
3 Ranvijay
4 Kapil
5 Shalini
6 Rakesh
7 Akshay
8 Sarah
9 Rocky

Example of Alias for Tables

For creating Alias for a table, a query will be:

Example - Creating Alias for table
SELECT *
FROM Employee as Emp;
Output
ID EmpName FlatNo City Country Gender Salary
1 Shankar A - 255 Delhi India male 25000
2 Sourabh A - 3 Delhi India male 30000
3 Ranvijay B - 526 Mumbai India male 15000
4 Kapil F - 255 Noida India male 25000
5 Shalini E - 205 Jaipur India female 18000
6 Rakesh J - 295 Faridabad India male 23000
7 Akshay A - 2055 Mumbai India male 21000
8 Sarah A - 55 New York US female 76000
9 Rocky A - 2505 Noida India male 28000