SQL UPDATE from Another Table: JOIN-Based Updates Explained
Update a table using values from another table. Covers MySQL, PostgreSQL, and SQL Server syntax differences with real examples.
The Problem
You need to update columns in one table using values from a related table. The syntax varies significantly between databases — this is one of the most common SQL portability headaches.
PostgreSQL: UPDATE ... FROM
UPDATE orders SET customer_name = c.name FROM customers c WHERE orders.customer_id = c.id;
PostgreSQL uses a FROM clause to join the source table. The WHERE clause links the two tables.
MySQL: UPDATE ... JOIN
UPDATE orders o INNER JOIN customers c ON o.customer_id = c.id SET o.customer_name = c.name;
MySQL puts the JOIN before SET. You can use INNER JOIN, LEFT JOIN, or multi-table joins.
SQL Server: UPDATE ... FROM (same as PostgreSQL)
UPDATE o SET o.customer_name = c.name FROM orders o INNER JOIN customers c ON o.customer_id = c.id;
SQL Server uses FROM with an alias on the target table. Very similar to PostgreSQL but requires the alias in UPDATE.
Safe Update Pattern
Always test with SELECT first: SELECT o.id, o.customer_name, c.name AS new_name FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.customer_name IS DISTINCT FROM c.name; Then wrap in a transaction and run the UPDATE.