coderz.py

Keep Coding Keep Cheering!

SQL Alias – AS Keyword

SQL Views
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:

IDNameMarks
1John64.12
2Sam72.50
3Alan45.90
4Rabecca86.37
5Anjali92.01

and Address_S table:

IDAddress
1DELHI
2MUMBAI
3CHENNAI
4NOIDA
5PANIPAT

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:
IDNameMarksAddress
1John64.12DELHI
2Sam72.50MUMBAI
3Alan45.90CHENNAI
4Rabecca86.37NOIDA
5Anjali92.01PANIPAT

Query for Column Alias:

SELECT  ID AS STUDENT_ID, NAME AS STUDENT_NAME
   FROM STUDENT
   WHERE MARKS > 80;
Output:
STUDENT_IDSTUDENT_Name
4Rabecca
5Anjali

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement