19. Using Aliases with AS

Core Idea: The AS keyword assigns temporary names to query output columns or source tables, improving readability and disambiguating identical column names across joins.

Column Aliases

SELECT ProductName AS ItemName, UnitPrice * Quantity AS LineTotal, UnitPrice * 0.18 AS TaxAmount FROM OrderItems;

Table Aliases in Joins

SELECT p.ProductName, c.CategoryName, p.UnitPrice FROM Products AS p INNER JOIN Categories AS c ON p.CategoryID = c.CategoryID;

Aliases with Spaces & Reserved Words

Wrap alias names in square brackets [...] or double quotes if they contain spaces or match SQL keywords:

SELECT FirstName + ' ' + LastName AS [Full Name], HireDate AS [Start Date] FROM Employees;

Advanced Deep Dive ADVANCED

Mandatory for Expressions: Computed columns, aggregate outputs, or subquery expressions require aliases if they will be accessed via derived tables or views.

Logical Processing Order: Column aliases defined in SELECT cannot be referenced in the WHERE or HAVING clause because WHERE is evaluated before SELECT. However, aliases can be used in ORDER BY.

Performance Impact: Aliasing is purely a syntactic construct evaluated at compile time; it incurs zero runtime performance overhead.

Self-Joins: Table aliases are strictly required when performing self-joins to reference the same table on both sides of a relationship.

next