A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

UPDATE with JOINs: Conditional Updates

College Depth 73 in the knowledge graph ☐ I know this ☆ Set as goal
338prerequisites beneath it
See this on the map →
SQL JoinsSQL: INSERT, UPDATE, and DELETE (DML)+2 more→
sql dml joins

Core Idea

UPDATE statements can reference other tables via JOINs to conditionally modify rows based on data in related tables, enabling complex data synchronization and audit logic.

Explainer

You already know how to UPDATE rows using a WHERE clause and how JOINs combine data from multiple tables. UPDATE with JOINs merges these two ideas: instead of filtering rows to update based only on the target table's own columns, you can bring in data from other tables to decide which rows to change and what values to set. This is essential whenever a modification depends on a relationship — "update all orders whose customer is in California" or "set the discount column based on the product's category."

The syntax varies by database, which is a practical detail worth knowing. In MySQL and SQL Server, you write it directly: `UPDATE orders JOIN customers ON orders.customer_id = customers.id SET orders.tax_rate = 0.0725 WHERE customers.state = 'CA'`. In PostgreSQL, the syntax uses a `FROM` clause: `UPDATE orders SET tax_rate = 0.0725 FROM customers WHERE orders.customer_id = customers.id AND customers.state = 'CA'`. The logic is identical — you are joining to filter and compute — but the syntax difference catches people who switch between databases.

A powerful pattern is using a joined table not just to filter but to supply values. For example, suppose you have a `price_updates` staging table with new prices loaded from a vendor feed. You can write `UPDATE products SET price = price_updates.new_price FROM price_updates WHERE products.sku = price_updates.sku` — a single statement that synchronizes thousands of rows. This is far more efficient than looping through rows one at a time and is the standard pattern for bulk data synchronization.

The biggest risk with UPDATE-JOIN is accidentally updating more rows than intended. If the join produces multiple matching rows for a single target row, the behavior is database-dependent — some will apply one arbitrary match, others will error. Always test your join as a SELECT first: replace the UPDATE/SET with a SELECT that shows which rows would be affected and what values they would receive. If the SELECT returns duplicates in the target table's primary key, your join conditions need tightening before you run the actual UPDATE.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Set Operations: Union, Intersection, and Complement → Relational Algebra → SQL: SELECT Statement and Basic Queries → SQL: WHERE Clause and Filtering → DELETE Statements: Removing Rows with Conditions → UPDATE with JOINs: Conditional Updates

Longest path: 74 steps · 338 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.