ANSI SQL / UniversalData Manipulation (DML)

How to UPDATE a Table From Another Table in SQL

Update values in one table based on matching columns or aggregations in a secondary table.

Quick SQL Solution
Transactional (DML Update)ANSI SQL / UniversalIntermediate
-- PostgreSQL / SQLite syntax:
UPDATE products p
SET price = n.new_price,
    updated_at = NOW()
FROM price_updates n
WHERE p.sku = n.sku;
When to use this scenario:You imported a CSV spreadsheet of updated product prices into a staging table and need to bulk update your live products table.

Step-by-Step Query Breakdown

1PostgreSQL syntax (UPDATE ... FROM)
UPDATE products p
SET
  price = u.price,
  updated_at = NOW()
FROM product_price_imports u
WHERE p.id = u.product_id;

Uses the clean and efficient PostgreSQL UPDATE ... FROM syntax.

2MySQL syntax (UPDATE ... JOIN)
UPDATE products p
JOIN product_price_imports u ON p.id = u.product_id
SET
  p.price = u.price,
  p.updated_at = NOW();

MySQL supports direct JOIN syntax in UPDATE statements.

3Universal ANSI SQL Subquery (Works on all engines)
UPDATE products
SET price = (
  SELECT price FROM product_price_imports WHERE product_price_imports.product_id = products.id
)
WHERE EXISTS (
  SELECT 1 FROM product_price_imports WHERE product_price_imports.product_id = products.id
);

Correlated subquery compatible with all SQL engines including older databases.

Critical SQL Pitfalls & Precautions

  • β€’In ANSI subqueries, always include the WHERE EXISTS clause; otherwise, rows without a match in the secondary table will have their columns overwritten with NULL!
Frequently Asked Questions

UPDATE From Another Table - Frequently Asked Questions

Common questions about transactional safety, indexing, and engine compatibility.

Only one of the matching rows will be used arbitrarily to update the primary row. Ensure the secondary table has unique matching keys.