Table Aliases
in SQL
Learn how to use short, meaningful nicknames for your tables to write cleaner, more readable SQL queries.
SQL
What Is a Table Alias?
A table alias is a temporary, short name you assign to a table within a SQL query. Instead of writing out the full table name every time you reference a column, you use a brief nickname that lasts only for the duration of that query.
The AS Keyword
SELECT e.first_name, e.salary
FROM employees AS e;
employees
The real table name in the database
AS
Optional keyword that introduces the alias
e
The alias: used everywhere else in the query
With vs Without Aliases
✕ Without Alias
SELECT employees.first_name,
employees.salary,
departments.name
FROM employees
JOIN departments
ON employees.dept_id
= departments.id;
✓ With Aliases
SELECT e.first_name,
e.salary,
d.name
FROM employees e
JOIN departments d
ON e.dept_id
= d.id;
Why Use Table Aliases?
1
Readability: Short aliases make complex joins and subqueries far easier to read.
2
Less typing: Replace order_line_items with oli and save keystrokes on every column reference.
3
Self-joins: Aliases are required when joining a table to itself, because the database needs two distinct names for the same table.
4
Disambiguation: When two tables share a column name (e.g., id), aliases clarify which table you mean.
The Core Rule
AS Is Optional
Explicit AS
FROM employees AS e
Implicit (space only)
FROM employees e
Table Alias vs Column Alias
| Table Alias | Column Alias | |
|---|---|---|
| Purpose | Nickname for a table | Nickname for a column or expression |
| Defined in | FROM / JOIN | SELECT |
| Used as prefix? | Yes: e.salary | No: appears in output header |
| Example | employees AS e | salary AS pay |
How to Apply Table Aliases
1
Identify your tables in the FROM and JOIN clauses.
2
Choose short, meaningful aliases: use the first letter or a common abbreviation (e.g., orders → o, order_items → oi).
3
Add the alias right after the table name: table_name alias (or use AS).
4
Replace every table reference in SELECT, WHERE, ON, ORDER BY, etc. with the alias.
5
Be consistent: once you alias a table, use the alias everywhere: don't mix full names and aliases.
Example 1: Simple Join
-- List each student and their school name
SELECT s.name,
s.grade,
sc.school_name
FROM students AS s
JOIN schools AS sc
ON s.school_id = sc.id;
s
Alias for students
sc
Alias for schools
Example 2: Three-Table Join
-- Orders with customer name and product title
SELECT c.name,
p.title,
o.quantity,
o.order_date
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.id
JOIN products AS p ON o.product_id = p.id;
Example 3: Self-Join
-- Find each employee and their manager's name
SELECT emp.name AS employee,
mgr.name AS manager
FROM employees AS emp
JOIN employees AS mgr
ON emp.manager_id = mgr.id;
How the Database Resolves Aliases
Choosing Good Alias Names
✓ First letter
employees → e, orders → o
✓ Abbreviation
order_items → oi, product_reviews → pr
✕ Meaningless
Avoid x, t1, t2: they tell readers nothing
Common Mistakes
✕ Using the full name after aliasing
Once you write employees AS e, you must use e everywhere. Writing employees.salary later causes an error in some databases.
✕ Alias clashes
Giving two tables the same alias (e.g., both aliased as a) makes the query ambiguous and will cause an error.
✕ Forgetting alias in self-joins
A self-join requires two different aliases. Without them, the database can't distinguish the two copies of the same table.
Quick Reference Summary
| Concept | Detail | Example |
|---|---|---|
| Syntax (explicit) | table AS alias | employees AS e |
| Syntax (implicit) | table alias | employees e |
| Scope | Single query only | Not stored in DB |
| Required | Self-joins | employees emp, employees mgr |
| Best practice | Always use aliases in joins | Improves readability |
Question 1 of 5
Which clause is used to define a table alias?
Table aliases are defined in the FROM clause (or JOIN clause). That's where you write table_name AS alias. All other clauses only use the alias.
Question 2 of 5
In which situation are table aliases mandatory (not just convenient)?
A self-join joins a table to itself. The database needs two distinct names for the same table, so aliases are required. In all other cases, aliases are optional but recommended.
Question 3 of 5
What is wrong with this query?
SELECT employees.name
FROM employees AS e
WHERE e.salary > 50000;
Once you alias employees AS e, some databases require you to use e everywhere. Mixing the full name (employees.name) with the alias (e.salary) is inconsistent and can cause errors.
Question 4 of 5
Which is the best alias for the table product_categories?
pc is a short, meaningful abbreviation of product_categories. Generic names like x or t1 tell readers nothing, and product_categories_alias defeats the purpose of brevity.
Question 5 of 5
Given the query below, what does mgr represent?
SELECT emp.name, mgr.name
FROM staff AS emp
JOIN staff AS mgr
ON emp.manager_id = mgr.id;
This is a self-join. Both emp and mgr are aliases for the same staff table. mgr represents the manager's row, matched by emp.manager_id = mgr.id.