- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0
SQL Alias:
The SQL Alias is a feature of the SQL that is supported by all the relational database management systems. The SQL alias is used to rename a table or the column of table temporarily.
The SQL alias is used when there are complex problems and the user wants to reduce the amount of the code for the query. This makes the SQL query easy to understand. The SQL alia is also used to give a shorter name to a table or a column that are having a complex name. The syntax of SQL alias for a table is as follows:
SYNTAX of SQL Alias for Table
Select column-name
From table-name
As alias-name;
Suppose you have the following table Employee and you want to rename it as emp:
Dept_No | Dept_Name | Emp_No | Emp_Name |
10 | Management | E01 | John Abraham |
20 | Management | E02 | Tim |
30 | Finance | E06 | Stuart |
40 | Finance | E11 | Faddy |
50 | IT | E25 | Kate |
To rename the table you will use the following query:
Select *from Employee as emp;
SYNTAX of SQL Alias for Column
The following is the syntax of SQL alias for renaming a column:
SELECT
Column-name as alias-name
From
Table-name;
Suppose you have the following table employee:
Dept_No | Dept_Name | Emp_No | Emp_Name |
10 | Management | E01 | John Abraham |
20 | Management | E02 | Tim |
30 | Finance | E06 | Stuart |
40 | Finance | E11 | Faddy |
50 | IT | E25 | Kate |
Suppose you want to rename the column Emp_Name of the above table then you will use the following query:
Select Emp_Name as EN from Employee;
This query will name the column Emp_Name of the table Employee as EN.
EXAMPLE of Alias in SQL Query
Suppose you have the following two tables Instructor and Student:
Dept_No | DEPT | Instructor-ID | Instructor-Name |
10 | Marketing | E01 | John Abraham |
20 | Management | E02 | Tim |
30 | Finance | E04 | Ali |
40 | Accounting | E11 | Faddy |
50 | IT | E25 | Kate |
80 | Software | E40 | Kathy |
Instructor
STUD_ID | NAME | Dept_No | DEPT |
100 | Stuart | 10 | Marketing |
100 | Bills | 20 | Management |
140 | Jones | 30 | Finance |
110 | Johnny | 40 | Accounting |
110 | James | 60 | IT |
120 | Sam | 70 | Computer |
Student
Suppose you want to extract the data from both of the above table then you will use the following SQL query:
Select I. Dept_No, I. Name, S. Name from Instructor as I, Student as S where I. Dept_No = S. Dept_No;
The above query will generate the following result:
Dept_No | Instructor-Name | NAME |
10 | John Abraham | Stuart |
20 | Tim | Bills |
30 | Ali | Jones |
40 | Faddy | Johnny |
In the above example it can be seen that those instructor names and the student names are displayed whose Dept_No match. And also we renamed the Instructor table as I and Student table as S temporarily using alias.