Table of Contents

Introduction

SQL Server Alias is a temporary name assigned to a table, column, or expression within a SQL query. Aliases make query results easier to read and help simplify complex SQL statements. Instead of displaying long column names or repeatedly writing full table names, you can use short and meaningful names.

Aliases are especially useful when working with multiple tables, JOIN operations, calculated columns, aggregate functions, and subqueries. They improve query readability without changing the actual names of database tables or columns.

For example, a table may contain a column named FirstName. You can use an alias to display it as First Name in the query result.

SELECT FirstName AS [First Name]
FROM Employees;

The result displays the column heading as First Name, but the original column name in the database remains unchanged.

SQL Server supports two main types of aliases:

  • Column Alias: Assigns a temporary name to a column or expression.
  • Table Alias: Assigns a temporary short name to a table in a query.

Let’s explore how to use aliases in SQL Server with practical examples.

SQL Server Column Alias

A column alias gives a temporary name to a column in the result set. It is useful when the original column name is unclear, too long, or not suitable for displaying to users.

You can create a column alias using the AS keyword.

Column Alias Syntax

SELECT column_name AS alias_name
FROM table_name;

You can also omit the AS keyword:

SELECT column_name alias_name
FROM table_name;

Both statements produce the same column heading.

Column Alias Example

Suppose you have an Employees table with the following columns:

EmployeeID FirstName LastName Salary
1 John Smith 45000
2 David Miller 52000
3 Sarah Wilson 60000

You can assign a readable alias to the FirstName column:

SELECT
    FirstName AS EmployeeName,
    LastName AS Surname,
    Salary AS MonthlySalary
FROM Employees;

The output contains the following column headings:

EmployeeName Surname MonthlySalary
John Smith 45000
David Miller 52000
Sarah Wilson 60000

The aliases are only used in the query result. They do not rename the original columns in the Employees table.

Using Column Alias Without AS

The AS keyword is optional when defining a column alias in SQL Server. You can write the alias directly after the column name.

SELECT
    FirstName EmployeeName,
    LastName Surname,
    Salary MonthlySalary
FROM Employees;

This query returns the same result as the previous example.

Although omitting AS is valid, using it can make queries easier to understand, particularly when working with multiple columns and complex expressions.

SQL Server Alias with Spaces

Sometimes you may want to use spaces in an alias to make column headings more readable. SQL Server allows spaces in aliases when you enclose the alias in square brackets or double quotation marks.

For example:

SELECT
    FirstName AS [First Name],
    LastName AS [Last Name],
    Salary AS [Monthly Salary]
FROM Employees;

You can also use double quotation marks:

SELECT
    FirstName AS "First Name",
    LastName AS "Last Name"
FROM Employees;

Square brackets are commonly used in SQL Server for identifiers containing spaces or special characters.

SQL Server Alias with Calculated Columns

You can assign aliases to calculated expressions. This is useful when performing mathematical calculations or combining values from multiple columns.

For example, suppose the Employees table contains BasicSalary and Bonus columns. You can calculate the total salary and assign an alias to the result.

SELECT
    EmployeeID,
    BasicSalary,
    Bonus,
    BasicSalary + Bonus AS TotalSalary
FROM Employees;

Here, TotalSalary is an alias for the calculated expression.

You can also calculate a yearly salary:

SELECT
    EmployeeID,
    FirstName,
    Salary AS MonthlySalary,
    Salary * 12 AS YearlySalary
FROM Employees;

The YearlySalary column is calculated when the query executes. No new column is created in the database.

SQL Server Alias with String Functions

Aliases can also be used with SQL Server string functions such as CONCAT(), UPPER(), LOWER(), and SUBSTRING().

For example, use CONCAT() to combine an employee’s first and last names:

SELECT
    EmployeeID,
    CONCAT(FirstName, ' ', LastName) AS FullName
FROM Employees;

The result contains a new output column named FullName.

You can also convert names to uppercase:

SELECT
    FirstName,
    UPPER(FirstName) AS UppercaseName
FROM Employees;

The alias provides a meaningful heading for the value returned by the function.

SQL Server Table Alias

A table alias assigns a temporary short name to a table within a SQL statement. It makes queries shorter and easier to read, especially when multiple tables are involved.

Table aliases are frequently used with JOIN operations, subqueries, and self-joins.

Table Alias Syntax

SELECT
    alias_name.column_name
FROM table_name AS alias_name;

The AS keyword is optional for table aliases as well.

SELECT
    alias_name.column_name
FROM table_name alias_name;

Table Alias Example

Consider an Employees table. You can assign the alias e to the table:

SELECT
    e.EmployeeID,
    e.FirstName,
    e.LastName,
    e.Salary
FROM Employees AS e;

In this query, e is the alias for the Employees table. Instead of writing Employees.EmployeeID, you can write e.EmployeeID.

The alias exists only within the current query. It does not change the table’s actual name.

SQL Server Alias with JOIN

Table aliases are particularly useful when joining two or more tables. They help identify which table each column belongs to and reduce repetitive table names.

Suppose you have two tables:

Employees

EmployeeID EmployeeName DepartmentID
1 John 10
2 David 20
3 Sarah 10

Departments

DepartmentID DepartmentName
10 IT
20 HR

You can use aliases to join these tables:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID;

In this example:

  • e represents the Employees table.
  • d represents the Departments table.
  • e.DepartmentID refers to the department ID from the Employees table.
  • d.DepartmentID refers to the department ID from the Departments table.

The aliases make it easier to understand the relationship between the two tables.

SQL Server Alias with Multiple JOIN Operations

You can use different aliases when joining multiple tables in a single query. Each table should have a clear and unique alias within the query.

Suppose an application contains Orders, Customers, and Employees tables. You can retrieve order details along with customer and employee information.

SELECT
    o.OrderID,
    c.CustomerName,
    e.FirstName AS EmployeeName,
    o.OrderDate
FROM Orders AS o
INNER JOIN Customers AS c
    ON o.CustomerID = c.CustomerID
INNER JOIN Employees AS e
    ON o.EmployeeID = e.EmployeeID;

Here, o, c, and e represent the Orders, Customers, and Employees tables.

This approach keeps the query organized and makes column references easier to identify.

SQL Server Alias with WHERE Clause

You can use table aliases in the WHERE clause to reference columns from the aliased table.

SELECT
    e.EmployeeID,
    e.FirstName,
    e.Salary
FROM Employees AS e
WHERE e.Salary > 50000;

The alias e is used to reference columns from the Employees table.

However, a column alias defined in the SELECT list generally cannot be referenced in the WHERE clause of the same query.

For example, the following query is invalid:

SELECT
    Salary * 12 AS YearlySalary
FROM Employees
WHERE YearlySalary > 600000;

This happens because SQL Server logically processes the WHERE clause before the SELECT list.

You can repeat the expression in the WHERE clause:

SELECT
    Salary * 12 AS YearlySalary
FROM Employees
WHERE Salary * 12 > 600000;

Alternatively, use a derived table:

SELECT *
FROM
(
    SELECT
        EmployeeID,
        Salary * 12 AS YearlySalary
    FROM Employees
) AS EmployeeSalary
WHERE YearlySalary > 600000;

SQL Server Alias with ORDER BY

SQL Server allows you to use a column alias in the ORDER BY clause. This makes it convenient to sort query results using a calculated or renamed column.

SELECT
    EmployeeID,
    FirstName,
    Salary * 12 AS YearlySalary
FROM Employees
ORDER BY YearlySalary DESC;

The query sorts employees by yearly salary, from highest to lowest.

You can also sort using an alias assigned to an existing column:

SELECT
    FirstName AS EmployeeName,
    Salary AS MonthlySalary
FROM Employees
ORDER BY MonthlySalary DESC;

SQL Server Alias with GROUP BY

When using aggregate functions, aliases make calculated output columns easier to understand. However, SQL Server does not generally allow a SELECT-list alias to be used in the GROUP BY clause of the same query.

For example:

SELECT
    DepartmentID,
    COUNT(*) AS TotalEmployees
FROM Employees
GROUP BY DepartmentID;

Here, TotalEmployees is an alias for the result of the COUNT() function.

You can use the alias in the ORDER BY clause:

SELECT
    DepartmentID,
    COUNT(*) AS TotalEmployees
FROM Employees
GROUP BY DepartmentID
ORDER BY TotalEmployees DESC;

The results are grouped by department and sorted by the number of employees.

SQL Server Alias with Subquery

Aliases are required for derived tables (subqueries used in the FROM clause). A derived table must have a name so that SQL Server can reference its result as a table source.

For example:

SELECT
    EmployeeData.EmployeeID,
    EmployeeData.EmployeeName,
    EmployeeData.TotalSalary
FROM
(
    SELECT
        EmployeeID,
        FirstName AS EmployeeName,
        Salary + Bonus AS TotalSalary
    FROM Employees
) AS EmployeeData;

In this query, EmployeeData is the alias for the derived table.

The outer query uses this alias to access the columns returned by the inner query.

SQL Server Alias with Aggregate Functions

You can assign aliases to aggregate function results such as COUNT(), SUM(), AVG(), MIN(), and MAX().

For example, calculate the average salary:

SELECT
    AVG(Salary) AS AverageSalary
FROM Employees;

Calculate the total salary by department:

SELECT
    DepartmentID,
    SUM(Salary) AS TotalDepartmentSalary,
    AVG(Salary) AS AverageDepartmentSalary,
    COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY DepartmentID;

These aliases make aggregate results more meaningful and easier to understand in reports.

SQL Server Alias with CASE Expression

The CASE expression allows you to return different values based on specified conditions. You can assign an alias to the result to provide a meaningful column heading.

SELECT
    EmployeeID,
    FirstName,
    Salary,
    CASE
        WHEN Salary >= 60000 THEN 'High Salary'
        WHEN Salary >= 40000 THEN 'Medium Salary'
        ELSE 'Low Salary'
    END AS SalaryCategory
FROM Employees;

The SalaryCategory alias represents the value returned by the CASE expression.

This technique is useful for creating categorized reports and conditional output columns.

SQL Server Alias with Common Table Expression (CTE)

A Common Table Expression (CTE) is a temporary named result set defined using the WITH keyword. You can assign aliases to its columns and use the CTE name in the following SQL statement.

WITH EmployeeSummary AS
(
    SELECT
        DepartmentID,
        COUNT(*) AS TotalEmployees,
        AVG(Salary) AS AverageSalary
    FROM Employees
    GROUP BY DepartmentID
)
SELECT
    DepartmentID,
    TotalEmployees,
    AverageSalary
FROM EmployeeSummary;

Here, EmployeeSummary is the CTE name, while TotalEmployees and AverageSalary are column aliases.

The CTE name can be referenced by the statement immediately following the CTE definition.

SQL Server Alias in UPDATE Statements

You can also use table aliases in UPDATE statements. This is particularly helpful when updating records using another table or joining multiple tables.

For example:

UPDATE e
SET e.Salary = e.Salary + 5000
FROM Employees AS e
WHERE e.DepartmentID = 10;

The alias e identifies the Employees table being updated.

You can also update a table using a JOIN:

UPDATE e
SET e.Salary = e.Salary + 3000
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'IT';

This statement increases the salary of employees who belong to the IT department.