UPDATE with JOIN in MySQL - Time & Space Complexity
When we update data in one table based on matching rows in another, itβs important to know how the work grows as data grows.
We want to understand how the time to complete the update changes when the tables get bigger.
Analyze the time complexity of the following code snippet.
UPDATE orders o
JOIN customers c ON o.customer_id = c.id
SET o.status = 'processed'
WHERE c.region = 'North';
This code updates the status of orders for customers in the 'North' region by joining the orders and customers tables.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning and matching rows between the orders and customers tables using the JOIN condition.
- How many times: For each order row, the database looks for matching customer rows to check the region condition.
As the number of orders and customers grows, the database must check more rows to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 orders, 5 customers | About 10 checks to find matching customers |
| 100 orders, 50 customers | About 100 checks, each matching a customer |
| 1000 orders, 500 customers | About 1000 checks, matching customers as needed |
Pattern observation: The work grows roughly in proportion to the number of orders, assuming efficient customer lookup.
Time Complexity: O(n)
This means the time to update grows roughly in direct proportion to the number of rows in the orders table.
[X] Wrong: "The update will always take the same time no matter how many rows are in the tables."
[OK] Correct: More rows mean more matching checks, so the time grows with data size, not stays constant.
Understanding how joins affect update speed helps you explain database performance clearly and shows you know how data size impacts work.
"What if the customers table had no index on the id column? How would the time complexity change?"
Practice
UPDATE with JOIN in MySQL?Solution
Step 1: Understand the purpose of UPDATE with JOIN
UPDATE with JOIN is used to change data in one table using related data from another table by matching rows.Step 2: Compare options
Only To update rows in one table based on matching rows in another table describes updating one table based on another, which matches the purpose.Final Answer:
To update rows in one table based on matching rows in another table -> Option CQuick Check:
UPDATE with JOIN = update one table using another [OK]
- Confusing UPDATE with JOIN as a SELECT operation
- Thinking UPDATE with JOIN deletes data
- Believing it creates new tables
table1 using table2 with a JOIN on id in MySQL?Solution
Step 1: Recall correct UPDATE with JOIN syntax
The correct syntax places JOIN after UPDATE, then ON condition, then SET clause.Step 2: Check each option
UPDATE table1 JOIN table2 ON table1.id = table2.id SET table1.value = table2.value; matches the correct syntax: UPDATE table1 JOIN table2 ON ... SET ...; others misuse JOIN placement or syntax.Final Answer:
UPDATE table1 JOIN table2 ON table1.id = table2.id SET table1.value = table2.value; -> Option AQuick Check:
UPDATE + JOIN + ON + SET order = B [OK]
- Placing JOIN after SET clause
- Using WHERE instead of ON for JOIN condition
- Separating tables with commas instead of JOIN
users(id, name, city_id)cities(id, city_name)What will this query do?
UPDATE users u JOIN cities c ON u.city_id = c.id SET u.name = CONCAT(u.name, ' - ', c.city_name) WHERE c.city_name = 'Paris';
Solution
Step 1: Understand the JOIN and SET clauses
The query joins users with cities on city_id, then updates user names by appending city name for users in Paris.Step 2: Analyze WHERE condition
Only users with city_name 'Paris' are updated, so their names get ' - Paris' appended.Final Answer:
Append ' - Paris' to the name of users living in Paris -> Option DQuick Check:
UPDATE with JOIN + WHERE filters = append city name [OK]
- Thinking it replaces all names with 'Paris'
- Confusing UPDATE with INSERT
- Assuming it deletes rows
UPDATE orders o JOIN customers c SET o.status = 'shipped' ON o.customer_id = c.id WHERE c.region = 'West';
Solution
Step 1: Check JOIN syntax order
ON clause must come immediately after JOIN, before SET clause.Step 2: Locate ON clause in query
ON is incorrectly placed after SET, causing syntax error.Final Answer:
The ON clause is after SET instead of after JOIN -> Option AQuick Check:
ON must follow JOIN, not SET [OK]
- Placing ON after SET clause
- Confusing WHERE and ON clauses
- Assuming JOIN needs INNER keyword
products(id, price, category_id)categories(id, discount_percent)You want to reduce the price of all products by their category discount. Which query correctly updates
products.price applying the discount using UPDATE with JOIN?Solution
Step 1: Write correct UPDATE with JOIN syntax
JOIN must come after UPDATE, then ON condition, then SET clause with calculation.Step 2: Verify discount calculation
Price is reduced by price * discount_percent / 100, which is correct formula.Step 3: Check other options
Other options misuse JOIN placement, use comma syntax instead of explicit JOIN, or have incorrect syntax/order and discount formulas.Final Answer:
UPDATE products p JOIN categories c ON p.category_id = c.id SET p.price = p.price - (p.price * c.discount_percent / 100); -> Option BQuick Check:
UPDATE + JOIN + ON + SET with correct formula = C [OK]
- Placing JOIN after SET clause
- Using WHERE instead of ON for JOIN
- Incorrect discount formula
Ready to prove it?
Pass the test and this pattern moves unknown
