SQL SELECT AS

Learning Objective: In this tutorial you will learn what is SQL SELECT AS and how to use it in SQL statement.

SQL SELECT AS is used to rename a table column into a meaningful name in the result set. There is no effect on the actual column name, this is temporary in nature only for the result set. This is also called column aliasing.

This allows the developer to make the column name in more presentable manner in the query result without modifying the actual column name.

SQL SELECT AS Syntax

The general syntax of the SQL SELECT AS is as follows.

SELECT column_name1 AS new_name1,
       column_name2 AS new_name2,
       ..........
       column_nameN AS new_nameN
from table_name
[ORDER BY expression [ ASC | DESC ]];

In this syntax,

  • column_name[1..N] – The name of the table column needs to provide new name in the result set.
  • new_name[1…N] – The new name for the existing column name.
  • table_name – The name of the table where you want to rename the column name.
  • ORDER BY expression [ASC|DESC] – Optional. This is used to sort the selected rows in ascending or descending order. ASC is used to sort in ascending order and DESC is used to sort in descending order.

SQL SELECT AS Example

Consider the below Employee table for our reference and example.

emp_id emp_name emp_gender dept_code emp_location
1510 Avinash Sirsath M 101 Mumbai
1511 Ramjan Ali M 105 Kolkata
1512 Piyanka Saha F 103 Kolkata
1513 Piyam mondal M 101 Kolkata
1514 Jitu Garg M 104 Mumbai

Let’s check the Employee Id, Employee Name and Gender from the above Employee table.

SELECT emp_id, emp_name, emp_gender
FROM Employee
Order by emp_id;

The result of the above query will be as follows.

emp_id emp_name emp_gender
1510 Avinash Sirsath M
1511 Ramjan Ali M
1512 Piyanka Saha F
1513 Piyam mondal M
1514 Jitu Garg M

Now you can provide meaningful name to the each column of this result set using SELECT AS as below.

SELECT emp_id AS "Employee ID", 
       emp_name AS "Employee Name", 
       emp_gender AS Gender
FROM Employee
Order by emp_id;

Now you can see in the result set that each of the column has been provided a meaningful name.

Employee ID Employee Name Gender
1510 Avinash Sirsath M
1511 Ramjan Ali M
1512 Piyanka Saha F
1513 Piyam mondal M
1514 Jitu Garg M

Summary: In this tutorial, you have learned how to use SQL SELECT AS to provide a new name to the table column in the result set.