Learn SQL
-- modifying.update_from

UPDATE ... FROM / JOIN in SQL

Update from another table without nuking rows

--concept

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.

--how it works
1

Two spellings

PG/SQLite: UPDATE … FROM + WHERE correlate. MySQL/MSSQL: UPDATE … JOIN … SET.

2

Correlate exactly

Join condition pins each target row to its source row. Missing condition cross-writes everything.

3

Preview as SELECT

Run the same join as SELECT, check count, then convert to UPDATE.

4

Transact + verify

BEGIN, write, check ROW_COUNT and spot rows, then COMMIT or ROLLBACK.

--visualization
WHERE pins each target to its source — safe
namenew_salary
Bob53000
Elena59000
Stage 1 / 1
PG: UPDATE…FROM + WHERE correlate. MySQL: UPDATE…JOIN…SET. Preview the SELECT first.
--example
query.sql
1UPDATE employees SET salary = salary + 1000 WHERE department = 'Sales' RETURNING name, salary;
Result
namesalary
Bob53000
Elena59000

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.

--variants

PG FROM form

Source joined in FROM, pinned in WHERE.

query.sql
1UPDATE employees e SET salary = b.new_pay FROM pay_bands b WHERE e.band = b.band RETURNING e.name, e.salary;
Result
namesalary
Alice86000
Bob53000
1FROM brings pay_bands alongside each employee.
2WHERE keeps only band-matched pairs.
3SET writes the looked-up pay; RETURNING confirms.

Scoped bump (safe)

Correlated condition limits blast radius.

query.sql
1UPDATE employees SET salary = salary + 1000 WHERE department = 'Sales' RETURNING name, salary;
Result
namesalary
Bob53000
Elena59000
1WHERE selects Bob + Elena only.
2Expression references old value (+1000).
3Four other rows untouched — verify count 2 before COMMIT.
--key facts
Two spellingsPG/SQLite FROM vs MySQL JOIN
Correlate or catastrophemissing condition updates everything
Preview ruleSELECT the join first, then convert to UPDATE
--use cases
Backfills from mapping tables: bands, grades, FX rates
Correlated price/raise rollouts scoped by join
--under the hood
UPDATE…FROM plans as a join — EXPLAIN the SELECT form to confirm hash/nested-loop choice before writing.
Portable fallback SET col = (SELECT …) works where JOIN-update is unsupported, one probe per row.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.