Alias is the temporary name given to the column or to the table for better understanding.
Alias can only be used within the query in which it is created and it does not affect the actual database or table.
AS keyword is used to create alias.
Syntax: To create alias of column.
SELECT column_name AS alias_name
FROM table_name;
DEMO STUDENTS TABLE:
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |
Example: To create alias for Roll_No as rn.
SELECT Roll_No AS rn, Name, Marks
FROM students;
This will create alias as shown below:
rn | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |
Creating alias for table name
Syntax:
SELECT column_name(s)
FROM table_name AS alias_name;
Example: To alias for table name.
SELECT * FROM
students AS std;
This will temporary change the table name to std as below:
Roll_No | Name | Marks |
1 | Dev | 35 |
2 | Ayush | 45 |
3 | Ram | 55 |
4 | Pyush | 65 |
5 | Nazim | 75 |