Overview

SQL Aliases are used to temporarily assign a different name to a table and columns headings while they are making a SELECT query. Often when we use Joins on table we use aliases at that time.

Aliases are also used to make tables and column names more readable while we are fetching data through comples queries.

Syntax

Syntax for Aliases for columns and tables are given below

SELECT column_name AS temp_column_name FROM table_name;
SELECT column_name FROM table_name AS temp_table_name;

Example

Lets us have a closer look with the help of following table

i) hostel_list containing list of hostels.

+----+-------------------+-----------+----------+
| id | name | occupancy | capacity |
+----+-------------------+-----------+----------+
|  1 | ASHOK BHAVAN | 2 | 1090 |
|  2 | KAILASH BHAVAN | 2 | 990 |
|  3 | RUDRAKSH BHAVAN | 2 | 990 |
|  4 | KAVERI BHAVAN | 1 | 690 |
|  5 | BHAGIRATHI BHAVAN | 4 | 2140 |
|  6 | NARMADA BHAVAN | 6 | 3240 |
|  7 | GANGA BHAVAN | 1 | 780 |
|  8 | YAMUNA BHAVAN | 8 | 4580 |
+----+-------------------+-----------+----------+

ii) student_list containing list of students.

+-----+------------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+------------------+--------+-----------+
|   1 | Ankur Rastogi | First | 4 |
|   2 | Amit Kumar | Second | 8 |
|   3 | Ram Kumar | Second | 8 |
|   4 | Shiv Saxena | First | 6 |
|   5 | Aman Aggarwal | Third | 3 |
|   6 | Nipun Gupta | Fourth | 2 |
|   7 | Arpit Gupta | Third | 1 |
|   8 | Shantanu Rastogi | Second | 3 |
|   9 | Ramesh Kumar | Third | 1 |
|  10 | Neelima Dhingra | Second | NULL |
|  11 | Anu Sibbal | Third | NULL |
+-----+------------------+--------+-----------+
SELECT name, hostel_id AS hid FROM student_list;
+------------------+------+
| name | hid |
+------------------+------+
| Ankur Rastogi | 4 |
| Amit Kumar | 8 |
| Ram Kumar | 8 |
| Shiv Saxena | 6 |
| Aman Aggarwal | 3 |
| Nipun Gupta | 2 |
| Arpit Gupta | 1 |
| Shantanu Rastogi | 3 |
| Ramesh Kumar | 1 |
| Neelima Dhingra | NULL |
| Anu Sibbal | NULL |
+------------------+------+
SELECT s.name AS student_name, h.name AS hostel_name, s.year FROM student_list AS s, hostel_list AS h WHERE s.hostel_id = h.id;

We can also write above query as

SELECT s.name AS student_name, h.name AS hostel_name, s.year FROM student_list s, hostel_list h WHERE s.hostel_id = h.id;
+------------------+-----------------+--------+
| student_name | hostel_name | year |
+------------------+-----------------+--------+
| Ankur Rastogi | KAVERI BHAVAN | First |
| Amit Kumar | YAMUNA BHAVAN | Second |
| Ram Kumar | YAMUNA BHAVAN | Second |
| Shiv Saxena | NARMADA BHAVAN | First |
| Aman Aggarwal | RUDRAKSH BHAVAN | Third |
| Nipun Gupta | KAILASH BHAVAN | Fourth |
| Arpit Gupta | ASHOK BHAVAN | Third |
| Shantanu Rastogi | RUDRAKSH BHAVAN | Second |
| Ramesh Kumar | ASHOK BHAVAN | Third |
+------------------+-----------------+--------+


This article is contributed by Ankur Rastogi on Fri Mar 25 2016 21:54:00 GMT+0530 (IST)

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.