LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 2116 min read

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.

What You Will Learn
  • 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.

Column Alias
SELECT
FirstName + ' ' + LastName AS EmployeeName,
Salary AS MonthlyPay
FROM Employees;
Result

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.

Table Alias
SELECT e.FirstName, e.Department, e.Salary
FROM Employees AS e
WHERE e.Salary > 60000
ORDER BY e.Salary DESC;
Result

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.

Aliases in a Join
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.OrderTotal
FROM Customers AS c
INNER JOIN Orders AS o ON c.CustomerID = o.CustomerID;
Result

Click Run to see what this code prints.

Short, Meaningful Aliases

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

Avoid These 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.

Next Lesson →

UPDATE Statement