Aliasing tables in a SELECT statement example
By: Jeremy Kadlec
The purpose for aliasing a table is to have a short abbreviation for the table. Typically a long table name can easily be aliased with one to three characters. By doing so the code is typically much shorter and easier to read when troubleshooting. Taking this small step will hopefully lead to a simpler next step with JOINs where table aliasing is key. Let's take a look at an example.
Let's use the same query from an earlier tutorial with a few minor modifications for table aliasing. In the example below, we are selecting the LoginID column aliased as 'Domain\LoginName' from the HumanResources.Employee table where the VacationHours column equals 8 and we are ordering the data by the HireDate in ascending order which is implied. What is different is that we are aliasing the HumanResources.Employee table as 'E' to simplify the code in preparation for the JOIN examples.
USE AdventureWorks; GO SELECT E.LoginID AS 'Domain\LoginName' FROM HumanResources.Employee E WHERE E.VacationHours = 8 ORDER BY E.HireDate; GO
Below is the sample result set: