Aliases
Learn how to use column and table aliases in SQL Server with AS, and why aliases become essential once you start writing joins.
Introduction
You have already seen the AS keyword used to rename columns in query output. Aliases go further than that, though — they can also rename entire tables within a query, which becomes essential once you start combining data from multiple tables. This lesson takes a closer look at both column and table aliases in SQL Server.
- How column aliases improve readability.
- How table aliases shorten and clarify multi-table queries.
- Why aliases become necessary once you start writing joins.
Column Aliases
A column alias temporarily renames a column (or expression) in the result set using AS. It does not change anything in the underlying table — it only affects how the output is labeled for that query.
SELECT FirstName + ' ' + LastName AS EmployeeName, Salary AS MonthlyPayFROM Employees;Click Run to see what this code prints.
Table Aliases
A table alias gives a table a short, temporary name for the duration of the query, using AS after the table name in the FROM clause (the AS keyword is optional here too). Once aliased, you refer to the table using the short name for the rest of the query.
SELECT e.FirstName, e.Department, e.SalaryFROM Employees AS eWHERE e.Salary > 60000ORDER BY e.Salary DESC;Click Run to see what this code prints.
Why Aliases Matter for Joins
Once a query references two or more tables, especially tables that share a column name like CustomerID, you must qualify which table each column comes from. Table aliases make this both possible and readable — without them, every column reference would need the full table name, making queries verbose and harder to scan.
CREATE TABLE Customers ( CustomerID INT IDENTITY(1,1) PRIMARY KEY, CustomerName VARCHAR(100));
CREATE TABLE Orders ( OrderID INT IDENTITY(1,1) PRIMARY KEY, CustomerID INT, OrderTotal DECIMAL(10,2));
SELECT c.CustomerName, o.OrderID, o.OrderTotalFROM Customers AS cINNER JOIN Orders AS o ON c.CustomerID = o.CustomerID;Click Run to see what this code prints.
Common convention is to use the first letter (or first few letters) of the table name as its alias, e.g. c for Customers, o for Orders. Keep aliases short but still recognizable.
Common Mistakes
- Referencing a table's original name after it has been aliased — once aliased, the original name is no longer valid within that query.
- Choosing alias names that are so short or generic they make the query harder to read, like using x and y for every table.
- Forgetting to qualify column names with the correct alias in a join, causing an "ambiguous column name" error.
- Reusing the same alias for two different tables in the same query.
Best Practices
- Use short, meaningful table aliases, especially in joins.
- Qualify every column reference with its table alias once more than one table is involved.
- Use AS for column aliases even though it is optional, for clarity.
- Keep alias naming consistent across a project, e.g. always e for Employees, o for Orders.
Frequently Asked Questions
No, T-SQL allows FROM Employees e without AS, but many style guides recommend including AS for readability and consistency with column aliases.
Yes. Once a table is aliased, you use the alias (not the original table name) everywhere else in that query, including WHERE, ORDER BY, and JOIN conditions.
No. Column aliases defined in the SELECT list are not available in WHERE, because WHERE is logically evaluated before SELECT. You would need to repeat the expression or use a subquery/CTE.
Key Takeaways
- Column aliases rename output columns using AS.
- Table aliases give tables a temporary short name for the query.
- Aliases are essential for clarity and correctness once joins are involved.
- Once a table is aliased, the original table name can no longer be used in that query.
- Column aliases cannot be referenced in the WHERE clause of the same query.
Summary
Aliases make queries shorter, clearer, and — once you start combining tables — often necessary. Next, you will shift from reading data to modifying it, starting with the UPDATE statement.