Sub topic 2
Why Aliases Matter When a Table Appears Twice
Prerequisites and the join model
This chapter assumes that you can write basic SELECT statements and use INNER JOIN and LEFT JOIN with an ON condition. You should also understand primary keys, foreign keys, and NULL.
A join matches rows from two table references. Usually, those references have different names, such as orders and customers. A self join is different: both references point to the same table. SQL still needs to know which reference you mean for every column.
A table alias is a temporary name for a table reference in one query. You define it after the table name:
FROM employees AS eThe alias e identifies that particular reference to employees. You can then qualify a column with e.column_name.
One table, two roles
Consider an employee table where manager_id stores the id of another employee:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INT REFERENCES employees(id)
);
INSERT INTO employees (id, name, manager_id) VALUES
(1, 'Ava', NULL),
(2, 'Ben', 1),
(3, 'Chen', 1),
(4, 'Dana', 2);Each row can have an employee role and a manager role. Both roles come from employees, but they represent different rows.
To query the manager's name, use two aliases:
SELECT
e.name AS employee,
m.name AS manager
FROM employees AS e
INNER JOIN employees AS m
ON e.manager_id = m.id;Here, e means “the employee row,” and m means “the manager row.” The join condition says that the employee's stored manager_id must equal the manager row's id.
The result is:
| employee | manager |
|---|---|
| Ben | Ava |
| Chen | Ava |
| Dana | Ben |
Ava does not appear because her manager_id is NULL. An INNER JOIN keeps only rows for which the condition finds a match.
Why qualification is required
Without aliases, the query would refer to employees twice. A column such as id could belong to either reference. The database could not reliably determine whether you meant the employee's id or the manager's id.
Aliases make the roles explicit:
e.idis the employee reference's identifier.e.manager_idis the employee reference's manager identifier.m.idis the manager reference's identifier.e.nameandm.nameselect names from different rows.
You can omit the optional keyword AS for table aliases:
FROM employees e
JOIN employees m
ON e.manager_id = m.idBoth forms are equivalent. Many teams use AS because it makes the alias easier to spot.
Common errors
A frequent mistake is joining the wrong columns:
ON e.manager_id = m.manager_idThis compares the employee's manager identifier with the manager row's own manager identifier. It does not look up the manager. For a parent lookup, compare the child foreign key with the parent primary key:
ON e.manager_id = m.idAnother mistake is giving both references unclear aliases, such as x and y. Short aliases are useful, but names that describe the role, such as employee and manager, can make a complex query easier to review.
The alias exists only for the statement where it is declared. It does not rename the table in the database.
Exercise
Which alias identifies the row whose name is returned as the manager: e or m?
Show answer
m. The query uses m.name, and m.id is the parent row matched by e.manager_id.