SQL Server

Using Table and Column Aliases in SQL Server

Learn how SQL Server table and column aliases make query results friendlier and JOINs easier to read without renaming database objects.

By JaviPublished 10 min readBeginner

When you combine dbo.Customers and dbo.Orders, both tables contain a CustomerId column. A query that refers to a shared name without saying which table owns it can be ambiguous. Aliases solve this and can also make output headings more useful. An alias is a temporary name used by one query; it does not rename the table or column in the database.

This lesson follows why JOIN results may repeat customer information and builds on the SELECT statement and JOIN basics.

Give output columns a friendly name

A column alias changes the heading in the result grid:

SQL query: Give output columns a friendly nameT-SQL
SELECT
    FirstName AS First,
    LastName AS Last
FROM dbo.Customers;
T-SQL4 linesRead-only query display. It is not connected to a database.

The table still stores FirstName and LastName; this query displays the selected values under First and Last. This is helpful for exports, reports, or expressions whose default heading is hard to read. Choose names that are clear to the people using the output and avoid implying a different meaning from the stored value.

An alias can name a calculated expression. For example, combine a first and last name for display:

SQL query: Give output columns a friendly name (2)T-SQL
SELECT
    FirstName + ' ' + LastName AS FullName
FROM dbo.Customers;
T-SQL3 linesRead-only query display. It is not connected to a database.

FullName is a label for the result expression, not a new column in the table. Likewise, a calculation can receive a useful heading:

SQL query: Give output columns a friendly name (3)T-SQL
SELECT
    Amount,
    Amount * 0.07 AS EstimatedTax
FROM dbo.Orders;
T-SQL4 linesRead-only query display. It is not connected to a database.

This is only an illustrative calculation; it is not tax advice and does not change the stored order total. SQL Server returns the computed value for this query. Nothing is written back to the table.

Give tables short query names

A table alias is a short name for a table reference inside a query. The following query uses c for Customers and o for Orders:

SQL query: Give tables short query namesT-SQL
SELECT
    c.CustomerId,
    c.FirstName,
    o.OrderId,
    o.Amount
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
    ON c.CustomerId = o.CustomerId;
T-SQL8 linesRead-only query display. It is not connected to a database.

The aliases make each selected column’s source visible. c.CustomerId is the customer key; o.CustomerId is the foreign key on an order. Their values match for the relationship, but they belong to different tables and answer different questions. Table aliases become especially helpful when a query joins several tables or repeats long schema-qualified names.

Both tables have CustomerId. In a join, writing only CustomerId can be ambiguous because SQL Server cannot infer which one you mean. Qualify it as c.CustomerId or o.CustomerId. Even where a column name is currently unique, consistently qualifying columns in multi-table queries makes later edits safer and makes the source easier to review.

  1. dbo.Customers AS c → c.CustomerId
  2. dbo.Orders AS o → o.OrderId
  3. FirstName + LastName AS FullName → result heading
Short names identify sources and result expressions

The AS keyword and alias scope

This series uses AS consistently because it makes the alias declaration easy to spot. SQL Server also permits table aliases without AS in ordinary table references, such as FROM dbo.Customers c. Both forms create a query alias. For clarity, use the same style throughout a query rather than switching back and forth. For column aliases, AS is also a clear way to separate the expression from its output name.

Aliases exist only for the current query. After the query finishes, c and o have no lasting effect; another statement must declare its own aliases. They do not change Object Explorer, the database schema, or names used by other queries. To permanently rename a column or table would be a schema change and is a different operation.

It helps to separate the two kinds of alias while reading a query. A table alias identifies where an input value comes from: c.FirstName means the FirstName column of the Customers reference named c. A column alias names an output value: FullName labels the expression returned by SELECT. The first makes the source clear; the second makes the result easier to consume. A single query often uses both, as the join example does for its tables and the calculated-name example does for its output.

When reviewing a join, read each qualified reference from left to right: alias, then column. In o.CustomerId, o is the Orders row being considered and CustomerId is the value used to relate it to a customer. This is especially helpful when multiple tables share generic names such as Id, Name, Status, or CreatedDate. Qualifying a column is not extra decoration; it documents the source and avoids a query that stops working when another table with the same column is added.

Output aliases also make downstream use friendlier. A spreadsheet user may prefer CustomerName over a raw expression, and a report may prefer OrderTotal to an unnamed calculation. Keep the output name accurate: an alias cannot make a tax estimate into an approved tax amount or a customer name into a unique identifier. If an output name contains spaces, SQL Server allows bracketed names, but simple names such as FullName are easier to reference later and avoid extra punctuation.

Think of aliases as part of the query’s small interface with whoever reads or consumes its output. A clear column heading can travel into a spreadsheet, chart, or downstream query, so prefer stable names that describe the returned value. For table aliases, favor a compact, consistent mapping and declare it near the table reference. When a query becomes longer, it is reasonable to replace a one-letter alias with a descriptive word; saving a few keystrokes is less useful than making the source obvious to a teammate. Whatever style you choose, qualify shared columns consistently so each reference remains unambiguous.

If you revisit a query months later, a consistent naming pattern can help you identify sources quickly. Avoid using c for Customers in one section and a different table elsewhere in the same statement. Aliases are local, but their meaning should stay stable throughout that statement. The best alias is one that shortens repeated references while preserving the reader’s ability to understand the query without guessing.

Before sharing a query, read its aliases as if you had not written it; unclear names are easier to catch that way.

That quick review also confirms that every selected key and expression is labeled in a way that makes sense outside the query editor.

An output alias can be useful in ORDER BY, which is processed after the SELECT list in SQL Server’s logical query order. For example:

SQL query: The AS keyword and alias scopeT-SQL
SELECT FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
ORDER BY FullName;
T-SQL3 linesRead-only query display. It is not connected to a database.

This connects to the earlier lessons on logical SELECT order and sorting with ORDER BY. Do not assume a SELECT alias is available in every clause: for example, WHERE is evaluated before the SELECT list’s aliases are established. Refer to the underlying expression or restructure the query when a filter must use a calculation.

Common mistakes and practice

Avoid aliases that are so short that nobody can understand them, and avoid changing the same table’s alias halfway through a query. c and o are reasonable in a compact two-table example when declared clearly; meaningful names such as customer and orders may be better in a long query. Qualify shared columns, especially keys used in a join. Do not mistake an output heading for a schema rename, and do not hide the source of a calculated value behind a vague label.

Practice: (1) display first and last names as First and Last; (2) create FullName; (3) show order amount and a 7% illustrative calculation as EstimatedTax; (4) join customer names to orders using table aliases; (5) sort by the FullName output alias; (6) explain why CustomerId should be qualified in a join.

Answers

SQL query: AnswersT-SQL
-- 1
SELECT FirstName AS First, LastName AS Last FROM dbo.Customers;
-- 2
SELECT FirstName + ' ' + LastName AS FullName FROM dbo.Customers;
-- 3
SELECT Amount, Amount * 0.07 AS EstimatedTax FROM dbo.Orders;
-- 4
SELECT c.FirstName, o.OrderId, o.Amount
FROM dbo.Customers AS c JOIN dbo.Orders AS o ON c.CustomerId = o.CustomerId;
-- 5
SELECT FirstName + ' ' + LastName AS FullName
FROM dbo.Customers ORDER BY FullName;
T-SQL12 linesRead-only query display. It is not connected to a database.

The SQL Server Foundations roadmap now leads into aggregate functions, where clear aliases will help label counts and totals.

The next step is to summarize data with aggregate functions.

Check what you learned

2 quick questions

Question 1 of 2What does a table alias provide in a query?
Question 2 of 2How can you distinguish same-named columns from joined tables?

SQL Server Foundations

Lesson 12 of 25

Continue to next lesson

Aggregate Functions in SQL Server: COUNT, SUM, AVG, MIN, and MAX

Continue to Lesson 13

Tags

  • SQL Server
  • SQL Fundamentals
  • SSMS
  • T-SQL