SQL
database
update-query
table-join
conditional-logic

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.

sql
1SELECT t.id, t.status AS old_status, s.new_status
2FROM target_table t
3JOIN source_table s ON s.id = t.id
4WHERE t.status <> s.new_status
5  AND s.is_active = true;

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.

sql
1BEGIN;
2
3UPDATE target_table t
4SET status = s.new_status,
5    updated_at = NOW()
6FROM source_table s
7WHERE s.id = t.id
8  AND t.status <> s.new_status
9  AND s.is_active = true;
10
11COMMIT;

Important detail: the join condition stays in WHERE, not in FROM join syntax.

MySQL Syntax

MySQL uses UPDATE with explicit JOIN in the statement.

sql
1START TRANSACTION;
2
3UPDATE target_table t
4JOIN source_table s ON s.id = t.id
5SET t.amount = s.correct_amount,
6    t.updated_at = NOW()
7WHERE t.amount <> s.correct_amount
8  AND s.ready_flag = 1;
9
10COMMIT;

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.

sql
1BEGIN TRAN;
2
3UPDATE t
4SET t.status = s.new_status,
5    t.updated_at = SYSDATETIME()
6FROM dbo.target_table t
7JOIN dbo.source_table s ON s.id = t.id
8WHERE t.status <> s.new_status
9  AND s.is_active = 1;
10
11COMMIT TRAN;

If you need audit output, SQL Server OUTPUT clause can capture changed rows.

Guardrails for Production Safety

Use practical guardrails every time:

  1. Run preview query and record expected row count.
  2. Run update in transaction.
  3. Compare affected rows to expected count.
  4. Commit only if counts match expectation.
  5. Otherwise rollback and investigate.

Example with row count validation in PostgreSQL:

sql
1BEGIN;
2
3WITH updated AS (
4  UPDATE target_table t
5  SET status = s.new_status
6  FROM source_table s
7  WHERE s.id = t.id
8    AND t.status <> s.new_status
9    AND s.is_active = true
10  RETURNING t.id
11)
12SELECT COUNT(*) AS updated_rows FROM updated;
13
14-- decide to COMMIT or ROLLBACK based on validated count

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:

sql
-- pseudo pattern for batched updates by id range
WHERE t.id BETWEEN 100000 AND 199999

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.

sql
WHERE t.status IS DISTINCT FROM s.new_status

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.

Course illustration
Course illustration

All Rights Reserved.