Update with Join
📝 SQLThis snippet updates rows in a table based on matching values from another table using a JOIN.
SQL
UPDATE employees e
JOIN departments d ON e.deptid = d.deptid
SET e.departmentname = d.departmentname
WHERE e.departmentname IS NULL OR e.departmentname <> d.departmentname;
Comments
That's a clean way to synchronize data between tables.
This is a clean way to synchronize data across related tables.
@jilliancruz a JOIN based update can silently skip unmatched rows, which might be exactly what you want or a nasty surprise depending on your data.