SQL-Column and Table Alias


In this tutorial, you have learned how to use the SQL Server alias including column alias and table alias.

AS Keyword

The As keyword is used as an alias. It is used to rename a column or a table. AS keyword is used to make table or column names more readable. The AS keyword is used to give an alias or change the name of a table or column on a temporary basis in SQL SELECT Query. This means an alias only exists for the duration of the SQL query. For example, you have a column named "Emp_Name" but you want to show it as "Employee Name". 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

SELECT Student_ID AS ID, Student_Name AS Name FROM Students;

To get full names of Students, you can concatenate the first name, space, and the last name using the concatenation+ operator as shown in the following query:

SELECT
first_name + ' ' + last_name 
FROM
Students
ORDER BY
first_name;

SQL Server returned the full name column as ( No column name) which is not meaningful in this case.

Now adding alias name to the column,

Alias For Table

SELECT
first_name + ' ' + last_name as FullName
FROM
Students
ORDER BY
first_name;

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>;

Example

SELECT E.student_id, E.Student_Name From Students AS E

Next