Pre-Requisites:SQL table
Aliases are the temporary names given to tables or columns for the purpose of a particular SQL query. It is used when the name of a column or table is used other than its original name, but the modified name is only temporary.
- Aliases are created to make table or column names more readable.
- The renaming is just a temporary change and the table name does not change in the original database.
- Aliases are useful when table or column names are big or not very readable.
- These are preferred when there is more than one table involved in a query.
Syntax for Column AliasÂ
SELECT column as alias_name FROM table_name;
column: fields in the table
alias_name: temporary alias name to be used inÂ
replacement of original column nameÂ
table_name: name of table
Parameter Explanation
The following table explains the arguments in detail:
- Column_Name: The column name can be defined as the column on which we are going to create an alias name.
- Alias_Name: It can be defined as a temporary name that we are going to assign for the column or table.Â
- AS: It is optional. If you have not specified it, there is no effect on the query execution.Â
 Syntax for Table Alias
SELECT column FROM table_name as alias_name;
column: fields in the tableÂ
table_name: name of table
alias_name: temporary alias name to be used in replacementÂ
of original table name
Lets see examples for SQL Aliases.Â
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');
Select * from Customer;
Output:
Â
Example 1: Column Alias
To fetch SSN from the customer table using CustomerID as an alias name.
Query:
SELECT CustomerID AS SSN FROM Customer;
Output:
Â
Example 2: Table Alias
Generally, table aliases are used to fetch the data from more than just a single table and connect them through field relations.
To fetch the CustomerName and Country of the customer with Age = 21.
Query:
SELECT s.CustomerName, d.Country
FROM Customer AS s, Customer
AS d WHERE s.Age=21 AND
s.CustomerID=d.CustomerID;
Output:
Â
Advantages of SQL Alias
- It is useful when you use the function in the query.
- It can also allow us to combine two or more columns.
- It is also useful when the column names are big or not readable.
- It is used to combine two or more columns.
Â
Please comment if you find anything incorrect or want to share more information about the topic discussed above.
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...