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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesSet Operations: Union, Intersection, and ComplementRelational AlgebraSQL: SELECT Statement and Basic QueriesSQL: WHERE Clause and FilteringDELETE Statements: Removing Rows with ConditionsUPDATE with JOINs: Conditional Updates

Longest path: 74 steps · 338 total prerequisite topics

Prerequisites (4)

Leads To (0)

No topics depend on this one yet.