Categories: DBMS

SQL Alias – AS Keyword

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

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago