SQL Aliases (AS Keyword) – Complete Guide with Examples (2026-27)
A SQL Alias is a temporary name given to a column or a table in a query using the AS keyword. Aliases make your query results easier to read, make long table names shorter to write, and are essential when using aggregate functions like COUNT, AVG, SUM, MIN, and MAX. The alias only exists for the duration of the query — it never changes the actual column or table name in the database. In this beginner-friendly guide you will learn column aliases, table aliases, aliases with aggregate functions, aliases in GROUP BY and ORDER BY, and the most common mistakes to avoid.
✅ What is a SQL Alias?
✅ A SQL alias is a temporary name assigned to a column or table in a query using the AS keyword.
✅ Column alias — renames a column in the query output. SELECT Name AS EmployeeName
✅ Table alias — gives a short name to a table for use within the same query. FROM Employees AS e
✅ The alias only exists for the duration of the query — it does not change the actual column or table name in the database.
✅ The AS keyword is optional in most databases — but always include it for readability.
→ Rename
COUNT(*) to TotalEmployees in the output→ Shorten
EmployeeManagementSystem to ems in a JOIN→ Combine first and last name into a single
FullName column→ Make aggregate function results readable:
AVG(Salary) AS AvgSalary
✅ Sample Employees Table – used in all examples below:-
| ID | Name | Department | Salary | City |
|---|---|---|---|---|
| 1 | ANNI | IT | 10000 | Mumbai |
| 2 | POOJA | IT | 9000 | Pune |
| 3 | RAJ | HR | 5000 | Delhi |
| 4 | SUJAN | HR | 8000 | Pune |
| 5 | MEENA | Sales | 12000 | Mumbai |
| 6 | PRAMOD | IT | 15000 | Bangalore |
| 7 | NILESH | Sales | 7000 | Delhi |
✅ SQL Alias Syntax
Column Alias Syntax
SELECT column_name AS alias_name
FROM table_name;
Table Alias Syntax
SELECT alias.column_name
FROM table_name AS alias;
✅ AS:- Keyword that assigns the alias. Optional but strongly recommended for readability.
✅ alias_name:- The temporary name. No spaces allowed unless wrapped in quotes or brackets.
SELECT Name AS EmployeeName and SELECT Name EmployeeName produce identical results. However, including AS makes your SQL instantly understandable to anyone reading it — always include it.
✅ Example 1 – Column Alias: Rename a Column in the Output
Rename the Name column to EmployeeName and Salary to MonthlySalary in the query output:
✅ SELECT Query:-
SELECT Name AS EmployeeName,
Salary AS MonthlySalary
FROM Employees;
✅ Result Show:-
| EmployeeName | MonthlySalary |
|---|---|
| ANNI | 10000 |
| POOJA | 9000 |
| RAJ | 5000 |
| SUJAN | 8000 |
| MEENA | 12000 |
| PRAMOD | 15000 |
| NILESH | 7000 |
Name and Salary. The alias only changes the column header in the output. The original table is not changed in any way.
✅ Example 2 – Column Alias with Spaces
If the alias name needs to contain spaces, wrap it in square brackets (SQL Server), backticks (MySQL), or double quotes (PostgreSQL/standard SQL):
✅ SQL Server / MS Access:-
SELECT Name AS [Employee Name],
Salary AS [Monthly Salary]
FROM Employees;
✅ MySQL:-
SELECT Name AS `Employee Name`,
Salary AS `Monthly Salary`
FROM Employees;
✅ PostgreSQL / Standard SQL:-
SELECT Name AS "Employee Name",
Salary AS "Monthly Salary"
FROM Employees;
✅ Result Show:-
| Employee Name | Monthly Salary |
|---|---|
| ANNI | 10000 |
| POOJA | 9000 |
Employee_Name works identically in all databases without any quoting.
✅ Example 3 – Multiple Column Aliases in One Query
Rename all selected columns with clear, readable aliases:
✅ SELECT Query:-
SELECT ID AS EmployeeID,
Name AS EmployeeName,
Department AS Dept,
Salary AS MonthlySalary,
City AS Location
FROM Employees
WHERE Department = 'IT';
✅ Result Show:-
| EmployeeID | EmployeeName | Dept | MonthlySalary | Location |
|---|---|---|---|---|
| 1 | ANNI | IT | 10000 | Mumbai |
| 2 | POOJA | IT | 9000 | Pune |
| 6 | PRAMOD | IT | 15000 | Bangalore |
✅ Example 4 – Alias on Aggregate Functions (Most Common Use)
Without an alias, aggregate function results have ugly headers like COUNT(*) or AVG(Salary). Aliases fix this:
✅ SELECT Query:-
SELECT
COUNT(*) AS TotalEmployees,
MIN(Salary) AS LowestSalary,
MAX(Salary) AS HighestSalary,
ROUND(AVG(Salary), 2) AS AverageSalary,
SUM(Salary) AS TotalSalaryBill
FROM Employees;
✅ Result Show:-
| TotalEmployees | LowestSalary | HighestSalary | AverageSalary | TotalSalaryBill |
|---|---|---|---|---|
| 7 | 5000 | 15000 | 9428.57 | 66000 |
COUNT(*) as a column header is confusing; TotalEmployees is immediately clear to anyone viewing the report.
✅ Example 5 – Alias in GROUP BY and ORDER BY
Use the alias in ORDER BY to sort by the aliased column. In most databases you can also reference aliases in GROUP BY:
✅ SELECT Query:-
SELECT Department,
COUNT(*) AS TotalEmployees,
ROUND(AVG(Salary), 2) AS AvgSalary
FROM Employees
GROUP BY Department
ORDER BY TotalEmployees DESC, AvgSalary DESC;
✅ Result Show:-
| Department | TotalEmployees | AvgSalary |
|---|---|---|
| IT | 3 | 11333.33 |
| HR | 2 | 6500.00 |
| Sales | 2 | 9500.00 |
✅ Example 6 – Table Alias (Shortening Long Table Names)
Table aliases shorten long table names for use throughout the query. Instead of writing Employees.Name repeatedly, write e.Name:
✅ SELECT Query:-
SELECT e.Name AS EmployeeName,
e.Department,
e.Salary AS MonthlySalary
FROM Employees AS e
WHERE e.Salary > 8000
ORDER BY e.Salary DESC;
✅ Result Show:-
| EmployeeName | Department | MonthlySalary |
|---|---|---|
| PRAMOD | IT | 15000 |
| MEENA | Sales | 12000 |
| ANNI | IT | 10000 |
| POOJA | IT | 9000 |
FROM Employees AS e), you must use the alias throughout the rest of the query. You cannot mix Employees.Name and e.Salary in the same query once an alias is assigned.
✅ Example 7 – Table Alias in JOIN Queries
Table aliases are most valuable in JOIN queries — they make the query much shorter and prevent ambiguity when both tables have a column with the same name:
✅ Sample Departments Table:-
| DeptID | DeptName | Location |
|---|---|---|
| 1 | IT | Mumbai |
| 2 | HR | Delhi |
| 3 | Sales | Pune |
✅ SELECT Query – JOIN with table aliases:-
SELECT e.Name AS EmployeeName,
e.Salary AS MonthlySalary,
d.DeptName AS Department,
d.Location AS DeptLocation
FROM Employees AS e
JOIN Departments AS d
ON e.Department = d.DeptName
WHERE e.Salary > 8000;
✅ Result Show:-
| EmployeeName | MonthlySalary | Department | DeptLocation |
|---|---|---|---|
| ANNI | 10000 | IT | Mumbai |
| POOJA | 9000 | IT | Mumbai |
| SUJAN | 8000 | HR | Delhi |
| MEENA | 12000 | Sales | Pune |
| PRAMOD | 15000 | IT | Mumbai |
Employees.Name, Departments.DeptName, Employees.Salary everywhere is verbose. With e and d, the query is shorter, cleaner, and easier to maintain.
✅ Example 8 – Alias with Concatenation (Computed Column)
Use an alias to name a computed or concatenated column — for example combining Name and City into one readable column:
✅ MySQL / PostgreSQL – Concatenation with alias:-
SELECT CONCAT(Name, ' - ', City) AS EmployeeLocation,
Salary AS MonthlySalary
FROM Employees
ORDER BY Salary DESC;
✅ SQL Server – Concatenation with alias:-
SELECT Name + ' - ' + City AS EmployeeLocation,
Salary AS MonthlySalary
FROM Employees
ORDER BY Salary DESC;
✅ Result Show:-
| EmployeeLocation | MonthlySalary |
|---|---|
| PRAMOD - Bangalore | 15000 |
| MEENA - Mumbai | 12000 |
| ANNI - Mumbai | 10000 |
| POOJA - Pune | 9000 |
| SUJAN - Pune | 8000 |
| NILESH - Delhi | 7000 |
| RAJ - Delhi | 5000 |
CONCAT(Name, ' - ', City) — long and confusing. With AS EmployeeLocation the output is immediately understandable. Aliases on computed columns are essential for clean reporting.
✅ Column Alias vs Table Alias – Quick Comparison
| Feature | Column Alias | Table Alias |
|---|---|---|
| What it renames | A column in the SELECT output | A table name for use within the query |
| Syntax | SELECT Name AS EmployeeName | FROM Employees AS e |
| Purpose | Readable output headers, aggregate naming | Shorter table references, JOIN readability |
| Duration | Query output only | Throughout the current query |
| Can be used in WHERE | ❌ No | ✅ Yes (must use alias once assigned) |
| Can be used in ORDER BY | ✅ Yes | ✅ Yes |
| Changes actual DB name | ❌ No | ❌ No |
| Required in self-JOIN | No | ✅ Yes — mandatory for self-joins |
✅ Key Points to Remember
✅ AS assigns a temporary alias — the actual column/table name in the database never changes.
✅ Column alias renames the output header — use it on every aggregate function for clear reporting.
✅ Table alias shortens table references — especially useful in JOINs and subqueries.
✅ AS is optional but always include it — omitting AS makes code harder to read.
✅ Column aliases can be used in ORDER BY — but NOT in WHERE or HAVING in most databases.
✅ Use underscores, not spaces, in aliases — Employee_Name works in all databases without special quoting.
✅ Once a table alias is set, use it consistently — you cannot mix Employees.Name and e.Salary in the same query.
✅ Table aliases are mandatory in self-JOINs — you must alias both copies of the table to distinguish them.
✅ Always alias computed columns — expressions like CONCAT(Name, City) must have an alias to be readable.
✅ Common SQL Alias Mistakes to Avoid
Wrong:
WHERE EmployeeName LIKE 'A%' — alias defined in SELECT is not available in WHERE.Correct:
WHERE Name LIKE 'A%' — use the original column name in WHERE.
Wrong:
SELECT Salary AS SELECT — SELECT is a reserved word, causes error.Correct:
SELECT Salary AS MonthlySalary — choose a non-reserved alias name.
Wrong:
SELECT Name AS Employee Name — causes a syntax error.Correct:
SELECT Name AS Employee_Name — use underscores, or wrap in brackets: AS [Employee Name].
Wrong:
FROM Employees AS e WHERE Employees.Salary > 5000 — once aliased, always use the alias.Correct:
FROM Employees AS e WHERE e.Salary > 5000
Without alias:
SELECT COUNT(*), AVG(Salary) FROM Employees — column headers are COUNT(*) and AVG(Salary). Very hard to read in reports.With alias:
SELECT COUNT(*) AS Total, AVG(Salary) AS AvgSalary FROM Employees — immediately clear.
✅ Frequently Asked Questions (FAQ)
SELECT Name AS EmployeeName FROM Employees AS e;. The alias replaces the column or table name in the query output and in subsequent references within the query.SELECT Name EmployeeName FROM Employees e; works the same as SELECT Name AS EmployeeName FROM Employees AS e;. However, always include AS for clarity — it makes your code instantly readable to anyone, including your future self.SELECT Salary AS MonthlyPay FROM Employees;. A table alias gives a shorter name to a table for use within the query: FROM Employees AS e. Table aliases are especially useful in JOINs to avoid repeating long table names and to resolve column ambiguity.AS [Employee Name]. MySQL uses backticks: AS `Employee Name`. PostgreSQL uses double quotes: AS "Employee Name". The best practice is to use underscores instead of spaces: AS Employee_Name — works in all databases without any special syntax.