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.
-- PostgreSQL / SQLite syntax:
UPDATE products p
SET price = n.new_price,
updated_at = NOW()
FROM price_updates n
WHERE p.sku = n.sku;Step-by-Step Query Breakdown
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.
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.
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!
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.
Related SQL Query Guides
Browse All SQL RecipesIdentify rows with duplicate column values using GROUP BY and HAVING count(*) > 1, and safely remove duplicates using ROW_NUMBER() CTEs.
Compare row-by-row logging (DELETE), fast table emptying with auto-increment reset (TRUNCATE), and complete table removal (DROP).
Atomically insert a new row or update an existing row if a unique constraint or primary key conflict is encountered.
Compute cumulative running totals over time or partitioned groups using SUM() OVER (ORDER BY ...).
Build a production database index on a table with millions of rows without locking out live INSERT, UPDATE, or DELETE operations.
Reset the next auto-generated primary key ID back to 1 or synchronize it with the maximum existing ID in the table.