Why UPDATE needs caution in SQL - Performance Analysis
When we run an UPDATE command in a database, it changes data in one or more rows.
We want to understand how the time it takes grows as the data size grows.
Analyze the time complexity of the following SQL UPDATE statement.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
This code increases the salary by 10% for all employees in the Sales department.
Look for repeated work done by the database engine.
- Primary operation: Scanning rows to find those matching the condition.
- How many times: Once for each row in the employees table.
As the number of employees grows, the database checks more rows to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to complete the UPDATE grows linearly with the number of rows in the table.
[X] Wrong: "UPDATE runs instantly no matter how big the table is."
[OK] Correct: The database must check each row to see if it matches the condition, so more rows mean more work and more time.
Understanding how UPDATE scales helps you write efficient queries and avoid surprises in real projects.
"What if we add an index on the department column? How would that change the time complexity?"
Practice
UPDATE statement without a WHERE clause?Solution
Step 1: Understand the role of
TheWHEREinUPDATEWHEREclause limits which rows get updated. Without it, all rows are affected.Step 2: Consider the effect of missing
WithoutWHEREWHERE, every row in the table changes, which can cause data loss or errors.Final Answer:
Because it will update all rows in the table, possibly causing unwanted changes. -> Option DQuick Check:
MissingWHEREupdates all rows [OK]
- Assuming UPDATE only changes one row by default
- Forgetting WHERE clause causes full table update
- Thinking UPDATE deletes data
Solution
Step 1: Recall correct UPDATE syntax
The correct syntax isUPDATE table SET column = value WHERE condition;.Step 2: Check each option
UPDATE employees SET salary = 5000 WHERE department = 10; matches the correct syntax exactly. Others miss keywords or punctuation.Final Answer:
UPDATE employees SET salary = 5000 WHERE department = 10; -> Option BQuick Check:
Correct UPDATE syntax uses SET and WHERE [OK]
- Omitting SET keyword
- Missing equal sign (=)
- Leaving out WHERE clause
products with rows:{id:1, price:100}, {id:2, price:200}, {id:3, price:300}What will be the result after running:
UPDATE products SET price = price + 50 WHERE id > 1;Then selecting all rows?
Solution
Step 1: Identify rows affected by WHERE clause
The conditionid > 1affects rows with id 2 and 3 only.Step 2: Calculate new prices for affected rows
Price for id 2: 200 + 50 = 250; Price for id 3: 300 + 50 = 350; id 1 remains 100.Final Answer:
[{id:1, price:100}, {id:2, price:250}, {id:3, price:350}] -> Option CQuick Check:
Only rows with id > 1 updated [OK]
- Updating all rows ignoring WHERE
- Updating wrong rows by misreading condition
- Changing values incorrectly
UPDATE employees SET salary = salary + 1000 WHERE;Solution
Step 1: Check the
TheWHEREclause syntaxWHEREclause must have a condition after it. Here it is empty, causing error.Step 2: Verify other parts of the statement
SETkeyword and table name are correct; salary can be updated.Final Answer:
TheWHEREclause is incomplete and causes a syntax error. -> Option AQuick Check:
Incomplete WHERE causes syntax error [OK]
- Leaving WHERE empty
- Forgetting condition after WHERE
- Assuming WHERE is optional but incomplete
Solution
Step 1: Identify the condition to limit rows
Only products with price less than 100 should be updated, soWHERE price < 100is needed.Step 2: Choose correct price increase method
Increasing by 10% means multiplying price by 1.10, not adding 10.Final Answer:
UPDATE products SET price = price * 1.10 WHERE price < 100; -> Option AQuick Check:
Use WHERE and correct math for safe updates [OK]
- Forgetting WHERE and updating all rows
- Adding fixed amount instead of percentage
- Using wrong condition in WHERE
Ready to prove it?
Pass the test and this pattern moves unknown
