UPDATE with WHERE clause in MySQL - Time & Space Complexity
When we update data in a database, the time it takes depends on how many rows we check and change.
We want to know how the work grows as the table gets bigger when using an UPDATE with a WHERE clause.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
This code increases the salary by 10% for all employees in the Sales department.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows to check the department value.
- How many times: Once for each row in the employees table.
As the number of employees grows, the database checks more rows to find those in Sales.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the number of rows in the table.
Time Complexity: O(n)
This means the time to update grows in a straight line as the table gets bigger.
[X] Wrong: "The UPDATE only touches the matching rows, so it runs in constant time."
[OK] Correct: The database must check every row to find matches, so the time grows with the table size.
Understanding how updates scale helps you explain database performance clearly and shows you know what happens behind the scenes.
"What if the department column has an index? How would the time complexity change?"
Practice
WHERE clause do in an UPDATE statement in MySQL?Solution
Step 1: Understand the purpose of UPDATE
TheUPDATEcommand changes data in existing rows of a table.Step 2: Role of WHERE clause
TheWHEREclause limits which rows get changed. Without it, all rows update.Final Answer:
It specifies which rows should be updated. -> Option AQuick Check:
WHERE limits rows to update [OK]
- Thinking WHERE creates or deletes rows
- Confusing UPDATE with SELECT
- Ignoring WHERE causes all rows to update
salary to 5000 for employees with id 10 in MySQL?Solution
Step 1: Identify correct UPDATE syntax
The correct order is: UPDATE table SET column = value WHERE condition.Step 2: Check each option
UPDATE employees SET salary = 5000 WHERE id = 10; follows correct syntax. Others have misplaced clauses or wrong operators.Final Answer:
UPDATE employees SET salary = 5000 WHERE id = 10; -> Option BQuick Check:
UPDATE SET column=value WHERE condition [OK]
- Placing WHERE before SET
- Using AND instead of WHERE
- Incorrect assignment syntax
products with columns id, name, and price, what will be the price of product with id = 3 after running this query?UPDATE products SET price = price + 10 WHERE id = 3;
Assume the original price for
id = 3 was 50.Solution
Step 1: Understand the UPDATE operation
The query adds 10 to the current price for the product whereid = 3.Step 2: Calculate new price
Original price is 50, so 50 + 10 = 60.Final Answer:
60 -> Option CQuick Check:
50 + 10 = 60 [OK]
- Ignoring WHERE and updating all rows
- Setting price to 10 instead of adding
- Confusing original and updated values
UPDATE users SET age = 30 WHERE;
Solution
Step 1: Analyze the query structure
The query has UPDATE, SET, and WHERE keywords but WHERE has no condition.Step 2: Identify the error
WHERE must be followed by a condition to specify which rows to update.Final Answer:
Missing condition after WHERE clause. -> Option DQuick Check:
WHERE requires condition [OK]
- Leaving WHERE empty
- Misplacing SET and WHERE
- Forgetting semicolon
orders with columns order_id, status, and amount. You want to increase the amount by 20% only for orders with status 'pending' and amount less than 100. Which query correctly does this?Solution
Step 1: Understand the condition
We want to update only rows wherestatusis 'pending' ANDamountis less than 100.Step 2: Calculate new amount
Increase by 20% means multiplyamountby 1.2.Step 3: Check options
UPDATE orders SET amount = amount * 1.2 WHERE status = 'pending' AND amount < 100; correctly uses AND and multiplies amount by 1.2. Others use OR or wrong calculations.Final Answer:
UPDATE orders SET amount = amount * 1.2 WHERE status = 'pending' AND amount < 100; -> Option AQuick Check:
AND condition + *1.2 [OK]
- Using OR instead of AND
- Adding fixed 20 instead of 20%
- Multiplying by 0.2 instead of 1.2
Ready to prove it?
Pass the test and this pattern moves unknown
