Open In App

SQL Server ALIASES

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table or column. Using aliases complex joins of multiple tables are easy to read and understand so that you perform other operations on columns and tables easily.

Prerequisites

To use SQL aliases you should have basic knowledge of –

  • SELECT Statement
  • Referencing database table and column names
  • Concept of Join

Column Aliases

Column aliases are temporary names that are given to the columns for specific queries this makes output data more readable and easy to understand especially when dealing with complex queries like join queries and also refers to columns more easily in GROUP BY, ORDER BY, and WHERE clauses. Column aliases are defined in the SELECT statement. Let’s see the example

SELECT employee_id ID, first_name AS FirstName, last_name AS LastName FROM employees;

In this above example the column aliases ID, FirstName, and LastName are assigned to the columns employee_id, first_name, and last_name. When the query is executed, the result set will display these aliases in the table instead of the original column names(employee_id,first_name,last_name) which the enhancing readability.

Output:

Column-Aliases

Column Aliases

Table Aliases

Table aliases are temporary name that are given to the table especialy when working with multiple tables in a query.Table aliase are defined in FROM clause of query after original table name using AS keyword.let see the example of table aliases:

SELECT e.employee_id, e.first_name, d.department_name

FROM employees AS e

JOIN departments AS d ON e.department_id = d.department_id;

In this above example, the table aliases e and d are assigned to the employees and departments tables, respectively. These aliases are then used to qualify the column names in the SELECT statement and the JOIN condition. Using table aliases you can make the query sort and also helps to avoid the error when column name are shared with tables.

Output:

Table-Aliases

Table Aliases

Benefits of Using Aliases

  1. Readability : Aliase improve the readablilty of SQL queries like Joining of tables.
  2. Avoiding Ambiguity : When you dealing with multiple tables in which columns having same name then aliases helps you to avoid ambiguity of query.
  3. Simplifying Query: Aliase can simplify the SQL query so it can easy to manage the query.

Conclusion

In SQL Server, Column and Table Aliases are very important tool that allows the user to rename the column or table name temporarly which is very beneficial when there is complex SQL queries to handel like joining multiple tables or clauses of GROUP BY , ORDER BY. It also helps to understand data to non-techniqle person.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads