update columns values with column of another table based on condition
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Updating one table from another table is a common data-maintenance task, but it is also a common source of accidental mass updates. The safest approach is to treat update statements as two-step operations: preview first, update second. Clear join conditions and transaction boundaries are the difference between controlled correction and production incident.
Core Pattern: Preview Then Update
Always run a SELECT with the same join and filter conditions before executing UPDATE.
This preview confirms row count and values. If preview output is unexpected, do not run update.
PostgreSQL Syntax
PostgreSQL uses UPDATE ... FROM for join-based updates.
Important detail: the join condition stays in WHERE, not in FROM join syntax.
MySQL Syntax
MySQL uses UPDATE with explicit JOIN in the statement.
Semantics are similar, but syntax differs. Use the form required by your engine.
SQL Server Syntax
SQL Server supports join updates with FROM and target alias.
If you need audit output, SQL Server OUTPUT clause can capture changed rows.
Guardrails for Production Safety
Use practical guardrails every time:
- Run preview query and record expected row count.
- Run update in transaction.
- Compare affected rows to expected count.
- Commit only if counts match expectation.
- Otherwise rollback and investigate.
Example with row count validation in PostgreSQL:
This pattern makes updates auditable and easier to review.
Performance Considerations
Join updates on large tables can lock many rows and pressure indexes. Optimize by:
- Indexing join keys on both tables.
- Updating only changed rows with inequality conditions.
- Chunking large corrections into batches.
Batch example idea:
Smaller transactions reduce lock duration and rollback cost.
Handling Null Logic Correctly
Comparison with NULL requires special handling. In PostgreSQL, use IS DISTINCT FROM to compare null-safe inequality.
Without null-safe checks, unchanged rows may still be updated or expected rows may be skipped.
Common Pitfalls
A common pitfall is running update statements without a preview query, especially during urgent data fixes. Another issue is missing or incorrect join predicates that create unintended many-to-many updates. Teams also often skip transaction wrappers, making rollback difficult when results are wrong. Null handling mistakes are frequent when using simple inequality comparisons across nullable columns. Finally, scripts that update unchanged rows add write load, trigger unnecessary replication traffic, and complicate audit history.
Summary
- Preview rows first using the exact same join and filters as the update.
- Use dialect-appropriate join update syntax for your SQL engine.
- Execute updates inside transactions with row-count validation.
- Add null-safe comparison logic when columns can be null.
- Optimize joins and batch size to reduce lock and performance risk.

