Sub topic 7
When Several Rows Match One Target Row
This chapter assumes you know SELECT, WHERE, INNER JOIN, single-table UPDATE and DELETE, aliases, and primary and foreign keys. The examples use the products and price_updates tables from the earlier schema.
The matching rule
UPDATE ... FROM joins each target row to rows from the FROM clause. A target row can match zero, one, or many source rows.
- With zero matches, the target row is unchanged.
- With one match, PostgreSQL uses that source row.
- With several matches, PostgreSQL updates the target row once, using one matching source row. Which row it chooses is not predictable.
For example, suppose two price updates exist for product 10:
| product_id | new_price | created_at |
|---|---|---|
| 10 | 19.99 | 2026-01-01 |
| 10 | 21.99 | 2026-02-01 |
This statement does not reliably choose either price:
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;The result can change after a plan change, an index change, or a database upgrade. An ORDER BY outside this statement does not fix the choice.
Make the source one row per target
A common solution is to reduce the source rows before joining. This example chooses the newest price update for each product:
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;DISTINCT ON keeps the first row in each product_id group. The matching ORDER BY makes “first” mean newest by created_at.
If timestamps can tie, add a unique tie-breaker, such as an update ID:
ORDER BY product_id, created_at DESC, id DESCAnother option is aggregation. Use it when the business rule is to calculate one value, rather than choose one row:
UPDATE products AS p
SET price = u.maximum_price
FROM (
SELECT product_id, max(new_price) AS maximum_price
FROM price_updates
GROUP BY product_id
) AS u
WHERE u.product_id = p.id;DELETE ... USING behaves differently
With DELETE ... USING, several matching source rows do not delete the target repeatedly. A target row is deleted once if at least one match exists:
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed';If a customer somehow appears several times in the join result, the matching order still disappears only once. However, duplicate matches can make the query slower and may reveal a data-model problem.
Detect problems before changing data
Preview the join and count matches:
SELECT p.id,
count(u.*) AS matching_updates,
min(u.new_price) AS lowest_price,
max(u.new_price) AS highest_price
FROM products AS p
JOIN price_updates AS u ON u.product_id = p.id
GROUP BY p.id
HAVING count(u.*) > 1;If the rule requires one source row, enforce it with a unique constraint or select one row explicitly. A correlated subquery is also deterministic when it includes ordering and LIMIT:
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
);The EXISTS condition prevents products without updates from receiving NULL.
Exercise
A product has three rows in price_updates. You need the newest price, and ties must be resolved by the greatest update ID. What must the source query contain?
Show answer
It must produce one row per product with ORDER BY productid, createdat DESC, id DESC, then use DISTINCT ON (productid), or use an equivalent window function with rownumber().