What is an Alias?
- An SQL alias is simply an alternative name that can be used to refer to a table or column within a SQL query.
- This is especially useful for large or complex queries.
- Alias is primarily used to provide a short alias name for a complexly named column or table.
- The AS keyword is used to create an alias for a table or column.
Syntax:
- Table Alias
SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];
- Column Alias
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
Example of Alias:
Consider the following Student table:
ID | Name | Marks |
1 | John | 64.12 |
2 | Sam | 72.50 |
3 | Alan | 45.90 |
4 | Rabecca | 86.37 |
5 | Anjali | 92.01 |
and Address_S table:
ID | Address |
---|---|
1 | DELHI |
2 | MUMBAI |
3 | CHENNAI |
4 | NOIDA |
5 | PANIPAT |
Query to fetch data from both tables using SQL Alias(Table Alias):
SELECT S.ID, S.NAME, S.MARKS, A.ADDRESS
FROM STudent AS S, ADDRESS_S AS A
WHERE S.ID = A.ID;
Output:
ID | Name | Marks | Address |
1 | John | 64.12 | DELHI |
2 | Sam | 72.50 | MUMBAI |
3 | Alan | 45.90 | CHENNAI |
4 | Rabecca | 86.37 | NOIDA |
5 | Anjali | 92.01 | PANIPAT |
Query for Column Alias:
SELECT ID AS STUDENT_ID, NAME AS STUDENT_NAME
FROM STUDENT
WHERE MARKS > 80;
Output:
STUDENT_ID | STUDENT_Name |
4 | Rabecca |
5 | Anjali |
Note: also read about SQL Functions-II
Follow Me
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment