Sub topic 6
The Missing Join Condition and Other Common Mistakes
This chapter assumes that you know basic SELECT, WHERE, INNER JOIN, single-table UPDATE and DELETE, aliases, and primary and foreign keys. The examples use PostgreSQL syntax, where UPDATE ... FROM and DELETE ... USING select rows through another table.
Why the join condition matters
A join condition tells PostgreSQL which source row belongs to which target row. In an update, the target table contains the rows to change. In a delete, the target table contains the rows to remove.
With UPDATE ... FROM, the join condition normally appears in WHERE:
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;The condition connects price_updates.product_id to products.id. Without it, PostgreSQL has no relationship to follow.
The Cartesian-product mistake
This statement has no join condition:
-- Dangerous
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u;If price_updates contains at least one row, every product can match every staging row. PostgreSQL may update every product. When several staging rows exist, the value chosen for a product is not predictable.
The same problem occurs with DELETE ... USING:
-- Dangerous
DELETE FROM orders AS o
USING customers AS c;If the customers table is nonempty, every order can match a customer. The statement can delete all orders, even when you intended to delete only orders belonging to closed customers.
Use a condition that relates the tables:
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed';A filter such as c.status = 'closed' is not a replacement for the relationship. You usually need both the relationship and the business rule.
Other common mistakes
| Mistake | Typical result | Safer practice |
|---|---|---|
| Missing table relationship | Many or all target rows change | Join target and source keys |
| Filtering only the source table | Unrelated target rows may match | Include the key relationship |
| Several source rows per target | Chosen update value is unpredictable | Reduce source rows to one per target |
| Wrong alias | PostgreSQL reports a reference error | Use the declared alias everywhere |
| Qualifying the left side of SET | PostgreSQL rejects `p.price = ...` | Write `SET price = ...` |
If a target table has an alias, use that alias in expressions:
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;The left side of SET is different. Write SET price, not SET p.price.
Several matches per target row
Suppose a product has two rows in price_updates. The update condition matches both rows:
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;PostgreSQL updates the product once, but the source row it uses is not predictable. Do not depend on insertion order or an execution plan.
Choose one source row explicitly:
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;This keeps the newest price for each product. A unique constraint or a staging-table cleanup process can also prevent duplicate source rows.
Preview before changing data
First inspect the exact target rows:
SELECT p.id, p.price, u.new_price
FROM products AS p
JOIN price_updates AS u
ON u.product_id = p.id;Then run the modification inside a transaction:
BEGIN;
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id
RETURNING p.id, p.price;
-- Keep the changes only after checking the returned rows.
ROLLBACK;Use COMMIT instead of ROLLBACK when the result is correct.
Exercise
Why is c.status = 'closed' alone not enough in a DELETE ... USING statement, and what condition should accompany it?
Show answer
It only filters customer rows. Add the relationship, such as o.customer_id = c.id, so PostgreSQL deletes orders belonging to those customers rather than matching unrelated rows.