Sub topic 2
Aliases and what you can reference in SET
This chapter assumes you know basic SELECT, WHERE, and INNER JOIN, single-table UPDATE and DELETE, table aliases, and primary and foreign keys. The examples use the products, price_updates, customers, and orders tables from the topic's schema.
Aliases and what you can reference in SET
An alias is a short name for a table, written after the table name, such as products AS p. Once you alias a table, use the alias everywhere in the statement. The original table name is no longer valid in that statement.
In the SET clause, the column you assign must be unqualified. Write SET price = ..., not SET p.price = ..., because PostgreSQL rejects the qualified form. The value on the right side can reference any table listed in FROM.
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;The missing join condition and other common mistakes
If the WHERE clause does not link the tables, PostgreSQL pairs every target row with every row in the other table. The statement still runs. It sets every product's price from one arbitrary staging row.
-- Dangerous: no join condition
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u;Other common mistakes include:
- Using the original table name, such as
products.id, after aliasing it. - Leaving out the WHERE clause in
DELETE FROM orders AS o USING customers AS c. This deletes every order. - Writing the join condition in the wrong place, so a filter ends up in an unrelated clause.
When several rows match one target row
If price_updates holds two rows for one product, PostgreSQL does not raise an error. It applies one matching row, and you cannot predict which one. Keep only the newest row per product with DISTINCT ON:
UPDATE products AS p
SET price = u.new_price
FROM (
SELECT DISTINCT ON (product_id) product_id, new_price
FROM price_updates
ORDER BY product_id, created_at DESC
) AS u
WHERE u.product_id = p.id;Alternatives: IN, EXISTS, and correlated subqueries
Use IN when you only need to test membership:
DELETE FROM orders
WHERE customer_id IN (
SELECT id FROM customers WHERE status = 'closed'
);A correlated subquery in SET copies a value from another table. Add a WHERE EXISTS filter so products without updates keep their current price instead of becoming NULL:
UPDATE products AS p
SET price = (
SELECT u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id
ORDER BY u.created_at DESC
LIMIT 1
)
WHERE EXISTS (
SELECT 1 FROM price_updates AS u WHERE u.product_id = p.id
);Deleting rows that have no match
DELETE ... USING removes only rows that match. To remove rows with no match, use NOT EXISTS:
DELETE FROM orders AS o
WHERE NOT EXISTS (
SELECT 1 FROM customers AS c WHERE c.id = o.customer_id
);Exercise
Rewrite this statement so it uses IN instead of USING: delete every order whose customer has status 'closed'.
Show answer
DELETE FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE status = 'closed');