UPDATE ... FROM / JOIN in SQL
Update from another table without nuking rows
PostgreSQL/SQLite use UPDATE … FROM (join the source in FROM, correlate in WHERE); MySQL/SQL Server use UPDATE … JOIN. Both must correlate exactly — a missing join condition cross-updates every row. Portable fallback: SET col = (SELECT …) correlated subquery where JOIN-update is unsupported.
Two spellings
PG/SQLite: UPDATE … FROM + WHERE correlate. MySQL/MSSQL: UPDATE … JOIN … SET.
Correlate exactly
Join condition pins each target row to its source row. Missing condition cross-writes everything.
Preview as SELECT
Run the same join as SELECT, check count, then convert to UPDATE.
Transact + verify
BEGIN, write, check ROW_COUNT and spot rows, then COMMIT or ROLLBACK.
1UPDATE employees SET salary = salary + 1000 WHERE department = 'Sales' RETURNING name, salary;
| name | salary |
|---|---|
| Bob | 53000 |
| Elena | 59000 |
Only Sales rows match the correlated condition, so only Bob and Elena move +1000. Without the WHERE every employee would have been credited — preview the SELECT first.
PG FROM form
Source joined in FROM, pinned in WHERE.
1UPDATE employees e SET salary = b.new_pay FROM pay_bands b WHERE e.band = b.band RETURNING e.name, e.salary;
| name | salary |
|---|---|
| Alice | 86000 |
| Bob | 53000 |
Scoped bump (safe)
Correlated condition limits blast radius.
1UPDATE employees SET salary = salary + 1000 WHERE department = 'Sales' RETURNING name, salary;
| name | salary |
|---|---|
| Bob | 53000 |
| Elena | 59000 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.