SQL Aliases
SQL Aliases:
These are used to give a temporary name for a column or table. These are used to make tables or columns more readable.
- These are used to rename the table or column temporarily. But it does not change in the original database.
- Aliases are used when the table names or column names are big and they are not readable
- Alias is used with the “AS” Keyword.
- It exists only for the duration of a particular query.
Alias Column Syntax:
select columnname AS aliasname from tablename;
Alias Table Syntax:
select columnname from tablename AS aliasname;
Alias Column Example:
Example:
Student Table
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
Changing one column name by Alias:
select sname AS studentname from student;
Output:
Student Table
| studentname |
| Abhinav |
| Ramya |
| Preetham |
| Nethranand |
| Naveen |
| Harshitha |
| Bindhu |
| Nandhini |
| Hashish |
| Rahul |
Select sid AS studentid from Student;
Output:
Student Table:
| studentid |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
Changing two column names by Alias:
Select sid AS studentid, sname AS studentname from student;
Output:
Student Table
| studentid | studentname |
| 1 | Abhinav |
| 2 | Ramya |
| 3 | Preetham |
| 4 | Nethranand |
| 5 | Naveen |
| 6 | Harshitha |
| 7 8 | Bindhu Nandhini |
| 9 | Hashish |
| 10 | Rahul |
Select sid AS studentid, sage AS studentage from student;
Output:
Student Table
| studentid | studentage |
| 1 | 22 |
| 2 | 24 |
| 3 | 21 |
| 4 | 21 |
| 5 | 23 |
| 6 | 22 |
| 7 | 26 |
| 8 | 23 |
| 9 | 22 |
| 10 | 21 |
Changing three column names by Alias:
Select sid AS studentid, sname AS studentname , sage AS studentage from student;
Output:
Student Table
| studentid | studentname | studentage |
| 1 | Abhinav | 22 |
| 2 | Ramya | 24 |
| 3 | Preetham | 21 |
| 4 | Nethranand | 21 |
| 5 | Naveen | 23 |
| 6 | Harshita | 22 |
| 7 | Bindu | 26 |
| 8 | Nandhini | 23 |
| 9 | Hashish | 22 |
| 10 | Rahul | 21 |
Using Multiple column names as one column name by Alias:
Method I: select * from Student Table AS Student Data;
SELECT sname , sid + ', ' + sage + ', ' + Phonenumber AS studentdetails
FROM student;
Output:
Student Table
| sname | studentdetails |
| Abhinav | 1, 22, Male, 9895678909 |
| Ramya | 2, 24, Female, 6687654634 |
| Preetham | 3, 21, Male, 9867546453 |
| Nethranand | 4, 21, Male, 7675643423 |
| Naveen | 5, 23, Male, 6567784532 |
| Harshita | 6, 22, Female, 9867546231 |
| Bindu | 7, 26, Female, 6563412768 |
| Nandhini | 8, 23, Female, 6785674839 |
| Hashish | 9, 22, Male, 9453215052 |
| Rahul | 10, 21, Male, 9998989898 |
Method II:
Using concat operator
SELECT sname, CONCAT(sid, ', ', sage, ', ', sgender, ‘,’ Phonenumber) AS studentdetails
FROM Customers;
Output:
Student Table
| sname | studentdetails |
| Abhinav | 1, 22, Male, 9895678909 |
| Ramya | 2, 24, Female, 6687654634 |
| Preetham | 3, 21, Male, 9867546453 |
| Nethranand | 4, 21, Male, 7675643423 |
| Naveen | 5, 23, Male, 6567784532 |
| Harshita | 6, 22, Female, 9867546231 |
| Bindu | 7, 26, Female, 6563412768 |
| Nandhini | 8, 23, Female, 6785674839 |
| Hashish | 9, 22, Male, 9453215052 |
| Rahul | 10, 21, Male, 9998989898 |
Method III:
SELECT sname, (sid || ', ' || sage || ' ' || sgender||',' || Phonenumber) AS studentdetails from student;
Output:
Student Table
| sname | studentdetails |
| Abhinav | 1, 22, Male, 9895678909 |
| Ramya | 2, 24, Female, 6687654634 |
| Preetham | 3, 21, Male, 9867546453 |
| Nethranand | 4, 21, Male, 7675643423 |
| Naveen | 5, 23, Male, 6567784532 |
| Harshita | 6, 22, Female, 9867546231 |
| Bindu | 7, 26, Female, 6563412768 |
| Nandhini | 8, 23, Female, 6785674839 |
| Hashish | 9, 22, Male, 9453215052 |
| Rahul | 10, 21, Male, 9998989898 |
Alias Table Example:
Change Student Table name as Student Data
select * from Student Table AS Student Data;
Output:
Student Data
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
Change Student Table as Student Details
select * from Student Table AS Student Details;
Output:
Student Details
| sid | sname | sage | sgender | Phonenumber |
| 1 | Abhinav | 22 | Male | 9895678909 |
| 2 | Ramya | 24 | Female | 6687654634 |
| 3 | Preetham | 21 | Male | 9867546453 |
| 4 | Nethranand | 21 | Male | 7675643423 |
| 5 | Naveen | 23 | Male | 6567784532 |
| 6 | Harshita | 22 | Female | 9867546231 |
| 7 | Bindu | 26 | Female | 6563412768 |
| 8 | Nandhini | 23 | Female | 6785674839 |
| 9 | Hashish | 22 | Male | 9453215052 |
| 10 | Rahul | 21 | Male | 9998989898 |
