AS Keyword
The AS keyword is used to give an alias or change the name of a table or column on a temporary basis.
This means an alias only exists for the duration of the query.
For example, you have a column named "Emp_Dept" but you want to show it as "Employee Department". So, using an alias, we can achieve it.
Alias For Column
When we want to change the name of a column, you can achieve it using the following syntax.
Syntax
SELECT <COLUMN_NAME> AS <NEW_NAME>, <COLUMN_NAME> AS <NEW_NAME> FROM <TABLE>;
Example 1
SELECT Emp_ID AS ID, Emp_Name AS Name FROM Employee;
Example 2
If your alias name contains space or any reserved word in SQL, you must give it brackets or double quotes.
SELECT Emp_Name AS Name, Emp_Dept AS [Department Name] FROM Employee;
Example 3
SELECT Emp_Name, Add1 + ', ' + Add2 + ' ' + City + ', ' + Pincode AS Address FROM Employee;
Alias For Table
Sometimes we have to get the data from multiple tables, so if the column names are the same in multiple tables, at that time you have to specify the table name or alias name.
When we want to change the name of a table, you can achieve it using the following syntax.
Syntax
SELECT <TABLE_ALIAS>.<COLUMN_NAME_1>, <TABLE_ALIAS>.<COLUMN_NAME_2> FROM <TABLE_NAME> AS <TABLE_ALIAS>;
Example1
SELECT E.Emp_ID,E.Emp_Name From Employee AS E
Example2
SELECT E.Emp_ID,E.Emp_Name,D.Dept_Name,D.Dept_Id From Employee AS E
Join Department AS D On D.Dept_Id = E.Dept_Id
Summary
The As keyword is used as an alias. It is used to rename a column or a table.