Aliasing and AS Operator in SQL
For Complete YouTube Video: Click Here
In this class, we will try to understand Aliasing and AS Operator in SQL.
We have already discussed the concepts of CARTESIAN PRODUCT in SQL.
Table of Contents
Aliasing and AS Operator in SQL
Aliases are the temporary names given to columns.
To understand this, we will consider the tables below.
In the above table, the student and departments table have the same column names, ‘name,’ and dnumber.
We use the same query that we discussed in the previous class.
Write a Query to find the names of the students who belong to the CSE Department?
The query to get the students belonging to the CSE department is as shown below.
SELECT name FROM student, departments
WHERE dnumber = dnumber AND name = ‘CSE’;
The above query will produce an error because the column names are ambiguous to understand for the SQL.
The SQL will not understand which column must be considered for the condition name = “CSE’.
The output of the query is as shown below.
We do aliasing for the tables by providing the space after the table’s name.
Now, we use the alias name of the tables with columns separated by the dot, as shown below.
SELECT s.name FROM student s, departments d WHERE s.dnumber = d.dnumber AND d.name = ‘CSE’;
The output of the query is as shown below.
Aliasing column name using AS
To alias the table name, we don’t use the AS operator.
To alias the column names, we have to use the AS operator.
The query using the AS operator is as shown below.
SELECT s.name AS stuname FROM student s, departments d WHERE s.dnumber = d.dnumber AND d.name = ‘CSE’;
In the query’s output, SQL displays the column name as stuname instead of ‘name.’