SQL Tutorial – Lesson 21

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.

💡 Real-world uses of SQL aliases:
→ 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:-

Employees
IDNameDepartmentSalaryCity
1ANNIIT10000Mumbai
2POOJAIT9000Pune
3RAJHR5000Delhi
4SUJANHR8000Pune
5MEENASales12000Mumbai
6PRAMODIT15000Bangalore
7NILESHSales7000Delhi

✅ 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.

ℹ️ AS is optional: 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:-

Result – Renamed Columns
EmployeeNameMonthlySalary
ANNI10000
POOJA9000
RAJ5000
SUJAN8000
MEENA12000
PRAMOD15000
NILESH7000
ℹ️ Explanation: The actual database columns are still called 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:-

Result – Aliases with Spaces
Employee NameMonthly Salary
ANNI10000
POOJA9000
💡 Best practice: Avoid spaces in alias names — they add syntax complexity across databases. Use underscores instead: 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:-

Result – IT Employees with All Aliases
EmployeeIDEmployeeNameDeptMonthlySalaryLocation
1ANNIIT10000Mumbai
2POOJAIT9000Pune
6PRAMODIT15000Bangalore

✅ 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:-

Result – Company Salary Summary
TotalEmployeesLowestSalaryHighestSalaryAverageSalaryTotalSalaryBill
75000150009428.5766000
💡 This is the most important use of aliases — every aggregate function result should have a descriptive alias. 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:-

Result – Department Summary Sorted by Employee Count
DepartmentTotalEmployeesAvgSalary
IT311333.33
HR26500.00
Sales29500.00
⚠️ Cannot use column alias in WHERE: Column aliases defined in SELECT are NOT available in the WHERE clause — WHERE is evaluated before SELECT. Use the alias only in ORDER BY (and GROUP BY in most databases). To filter by an aliased value use HAVING or a subquery.

✅ 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:-

Result – High-Earning Employees (using table alias 'e')
EmployeeNameDepartmentMonthlySalary
PRAMODIT15000
MEENASales12000
ANNIIT10000
POOJAIT9000
ℹ️ Table alias rule: Once you assign a table alias (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:-

Departments
DeptIDDeptNameLocation
1ITMumbai
2HRDelhi
3SalesPune

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:-

Result – Employees and Department Info via JOIN
EmployeeNameMonthlySalaryDepartmentDeptLocation
ANNI10000ITMumbai
POOJA9000ITMumbai
SUJAN8000HRDelhi
MEENA12000SalesPune
PRAMOD15000ITMumbai
💡 Why table aliases matter in JOINs: Without aliases, writing 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:-

Result – Computed Column with Alias
EmployeeLocationMonthlySalary
PRAMOD - Bangalore15000
MEENA - Mumbai12000
ANNI - Mumbai10000
POOJA - Pune9000
SUJAN - Pune8000
NILESH - Delhi7000
RAJ - Delhi5000
ℹ️ Computed column alias: Without the alias, the column header would show 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

FeatureColumn AliasTable Alias
What it renamesA column in the SELECT outputA table name for use within the query
SyntaxSELECT Name AS EmployeeNameFROM Employees AS e
PurposeReadable output headers, aggregate namingShorter table references, JOIN readability
DurationQuery output onlyThroughout 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-JOINNo✅ 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 aliasesEmployee_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

❌ Mistake 1: Using a column alias in WHERE
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.
❌ Mistake 2: Alias name conflicts with SQL reserved keywords
Wrong: SELECT Salary AS SELECT — SELECT is a reserved word, causes error.
Correct: SELECT Salary AS MonthlySalary — choose a non-reserved alias name.
❌ Mistake 3: Spaces in alias without quotes
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].
❌ Mistake 4: Mixing table name and alias in the same query
Wrong: FROM Employees AS e WHERE Employees.Salary > 5000 — once aliased, always use the alias.
Correct: FROM Employees AS e WHERE e.Salary > 5000
❌ Mistake 5: Not aliasing aggregate function results
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)

What is a SQL alias?
A SQL alias is a temporary name given to a column or table in a query using the AS keyword. It makes query results easier to read and long queries shorter to write. The alias exists only for the duration of the query — it never changes the actual column or table name in the database.
What does AS do in SQL?
AS is the keyword used to assign an alias in SQL. It follows the column name or table name and precedes the alias: 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.
Is the AS keyword required for aliases in SQL?
No. AS is optional in most databases. 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.
Can I use a SQL alias in a WHERE clause?
No. Column aliases defined in SELECT cannot be used in WHERE because WHERE is evaluated before SELECT. To filter by an aliased value, use the original column name in WHERE. You can use column aliases in ORDER BY and GROUP BY in most databases (MySQL, PostgreSQL, SQL Server).
What is the difference between a column alias and a table alias?
A column alias renames a result column in the output: 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.
Can SQL alias names contain spaces?
Yes, but they must be wrapped in special characters depending on the database. SQL Server uses square brackets: 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.

✍️ About the Author: Pramod Behera

Pramod Behera is a SAP and SQL educator with 10+ years of experience in enterprise software and database systems. He founded LearnToSAP.com to help beginners learn SAP and SQL concepts through clear, practical, real-world examples. His tutorials have helped thousands of students and IT professionals across India and beyond.