UPDATE with FROM and RETURNING

Performs UPDATE with a FROM clause to join additional tables and uses RETURNING to retrieve modified rows for verification and auditing.

SqlData Integrity

Code

snippet
UPDATE products p
SET
    unit_price = p.unit_price * (1 + adj.price_adjustment),
    updated_at = CURRENT_TIMESTAMP,
    updated_by = current_user
FROM price_adjustments adj
WHERE p.product_id = adj.product_id
  AND adj.effective_date <= CURRENT_DATE
  AND adj.is_active = true
RETURNING
    p.product_id,
    p.product_name,
    p.unit_price AS new_price,
    (p.unit_price / (1 + adj.price_adjustment)) AS old_price,
    adj.price_adjustment AS adjustment;

What the code does

At its core this is a single UPDATE statement against products and price_adjustments.

Mechanically, the statements are built from: a WHERE clause filtering the rows one statement applies to; a RETURNING clause that hands the affected rows back to the caller.

Applying it

The products table is the working surface here.

Interactive Tools for This Topic

FAQ

Which tables does the SQL touch?
`products`, `price_adjustments` — referenced directly, no join needed.