Sub topic 5
Aliases and What You Can Reference in `SET`
This chapter assumes you know basic SELECT, WHERE, and INNER JOIN syntax, single-table UPDATE and DELETE, table aliases, and primary and foreign keys. The examples use PostgreSQL tables named products, price_updates, orders, and customers.
An alias is a temporary name for a table within one SQL statement. Aliases make joined statements shorter and remove ambiguity when several tables have columns with the same name.
Aliasing the target table
In UPDATE, place the alias immediately after the target table. Once you define an alias, use that alias everywhere you refer to the target table. The original table name is no longer available under its original name in that statement.
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;Here, p identifies rows in products, and u identifies rows in price_updates.
The left side of SET names a column being changed. Do not qualify it with the table alias. Write SET price, not SET p.price.
-- Correct
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;
-- Incorrect in PostgreSQL
UPDATE products AS p
SET p.price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;The right side of SET is an expression. It can use target columns without an alias and source columns with their aliases. For example, this applies a discount using the current target price:
UPDATE products AS p
SET price = p.price * 0.90
FROM price_updates AS u
WHERE u.product_id = p.id
AND u.new_price < p.price;The alias p is optional in the expression p.price, but using it can make the source of a column clearer.
Aliases in `DELETE ... USING`
DELETE ... USING follows the same alias rule. Alias the table whose rows will be deleted, then use the other table's alias in the join condition.
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed';This deletes rows from orders, not from customers. The table after DELETE FROM is always the target. Tables after USING only help select matching target rows.
| Clause | Role in the statement | Alias example |
|---|---|---|
| `DELETE FROM orders AS o` | Rows that can be deleted | `o` |
| `USING customers AS c` | Rows used to filter the target | `c` |
| `WHERE o.customer_id = c.id` | Relationship between the tables | `o`, `c` |
Self-joins and name visibility
A target table can also appear in FROM or USING when you need a second instance of the same table. Give the target and second instance different aliases.
DELETE FROM contacts AS older
USING contacts AS newer
WHERE older.email = newer.email
AND older.id > newer.id;This keeps the row with the smallest id for each email, assuming id increases over time. Without distinct aliases, PostgreSQL cannot tell which instance a column belongs to.
Exercise
In UPDATE products AS p SET price = u.new_price FROM price_updates AS u WHERE u.product_id = p.id, which alias identifies the rows that will be changed?
Show answer
p identifies the target rows in products. The alias u identifies source rows that provide new values.