Sub topic 4
Table aliases and qualified column names
This chapter assumes that you know basic SELECT, FROM, WHERE, and ORDER BY syntax, and that you understand primary keys and foreign keys. The examples use customers and orders, where orders.customer_id refers to customers.id.
When a query uses more than one table, a column name may be unclear. Both tables might contain a column called id, or both might contain name. A qualified column name identifies the table as well as the column:
table_name.column_nameFor example, customers.id means the id column from customers.
Why qualification matters
Consider this query:
SELECT id, name
FROM customers
INNER JOIN orders
ON orders.customer_id = customers.id;The database may reject it because id is present in both tables. It cannot safely choose between customers.id and orders.id. The name column might also be ambiguous if both tables have one.
Qualify columns whenever a query uses related tables:
SELECT customers.name, orders.id, orders.total
FROM customers
INNER JOIN orders
ON orders.customer_id = customers.id;Here, customers.name is the customer's name, while orders.id is the order identifier.
Using table aliases
A table alias is a shorter name assigned to a table for one query. Define it after the table name with AS:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;The aliases c and o make the query shorter. They also qualify every column clearly.
AS is optional for table aliases in many SQL systems:
FROM customers c
INNER JOIN orders oUsing AS often makes the alias easier to notice. After an alias is defined, use the alias instead of the original table name in that query.
| Expression | Meaning |
|---|---|
| `c.name` | The `name` column from the table aliased as `c` |
| `o.id` | The `id` column from the table aliased as `o` |
| `o.id AS order_id` | Return `o.id` with the output label `order_id` |
Choosing useful aliases
Use aliases that are short but understandable. Single letters such as c and o work well for two simple tables. Longer aliases can help when a query has many tables:
SELECT customer.name, order_record.id
FROM customers AS customer
INNER JOIN orders AS order_record
ON order_record.customer_id = customer.id;Avoid aliases that hide meaning. An alias such as x makes a complex query harder to read.
Aliases also help distinguish output names from source names. The following query keeps both identifiers visible:
SELECT
c.id AS customer_id,
c.name AS customer_name,
o.id AS order_id,
o.total AS order_total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id
ORDER BY o.order_date;A reliable reading method
When reading a joined query:
- List each table and its alias.
- Replace each qualified column with its source table.
- Check the
ONcondition to see which columns create the match. - Check output aliases, which change labels but do not change source data.
Exercise
In SELECT c.name, o.id FROM customers AS c INNER JOIN orders AS o ON o.customer_id = c.id, which expression refers to the customer's identifier?
Show answer
c.id refers to the customer's identifier. The selected expression o.id refers to the order's identifier.