01 / 21
AS alias

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

FROM clause table_name AS alias Define the alias here Use SELECT WHERE JOIN / ON

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

1. FROM employees AS e Register alias "e" → employees 2. JOIN departments AS d Register alias "d" → departments 3. ON e.dept_id = d.id 4. SELECT e.name, d.name Alias Lookup Table e → employees d → departments

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?

Question 2 of 5

In which situation are table aliases mandatory (not just convenient)?

Question 3 of 5

What is wrong with this query?

SELECT employees.name FROM employees AS e WHERE e.salary > 50000;

Question 4 of 5

Which is the best alias for the table product_categories?

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;