UPDATE with WHERE condition in SQL - Time & Space Complexity
When we update data in a database using a condition, it takes time to find which rows to change.
We want to know how the time needed grows as the data grows.
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 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
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 rows that match, so it takes the same time no matter how big the table is."
[OK] Correct: The database still looks at every row to find matches, so more rows mean more work.
Understanding how updates scale helps you explain database behavior clearly and shows you know what happens behind the scenes.
"What if we add an index on the department column? How would the time complexity change?"
Practice
What does the WHERE clause do in an UPDATE statement?
Solution
Step 1: Understand the purpose of UPDATE
The UPDATE statement changes data in existing rows of a table.Step 2: Role of WHERE clause in UPDATE
The WHERE clause specifies which rows should be changed, so only those rows are updated.Final Answer:
It limits which rows get updated. -> Option CQuick Check:
WHERE clause = row filter [OK]
- Thinking WHERE adds or deletes rows
- Believing UPDATE changes table structure
- Ignoring WHERE updates all rows
Which of the following is the correct syntax to update the salary to 5000 for employees with id 3?
UPDATE employees SET salary = 5000 WHERE id = 3;
Solution
Step 1: Check UPDATE syntax
The correct syntax requires SET before column assignment and WHERE for condition.Step 2: Validate each option
UPDATE employees SET salary = 5000 WHERE id = 3; uses SET and WHERE correctly; others miss SET or WHERE or have wrong syntax.Final Answer:
UPDATE employees SET salary = 5000 WHERE id = 3; -> Option DQuick Check:
SET + WHERE = correct UPDATE syntax [OK]
- Omitting SET keyword
- Forgetting WHERE clause
- Missing equal sign (=) in assignment
Given the table products:
id | name | price ---+---------+------- 1 | Pen | 10 2 | Pencil | 5 3 | Eraser | 3
What will be the result of this query?
UPDATE products SET price = price + 2 WHERE price < 10;
Solution
Step 1: Identify rows matching WHERE condition
Prices less than 10 are 5 and 3 (Pencil and Eraser).Step 2: Apply UPDATE only to matching rows
Only Pencil and Eraser prices increase by 2, Pen remains 10.Final Answer:
Only products with price less than 10 will have their price increased by 2. -> Option BQuick Check:
WHERE filters rows updated [OK]
- Assuming all rows update without WHERE
- Thinking price must be reset, not incremented
- Confusing syntax error with correct query
Find the error in this query:
UPDATE employees SET salary = 6000 WHERE;
Solution
Step 1: Analyze the WHERE clause
The WHERE clause is incomplete; it must have a condition after it.Step 2: Check other parts of query
SET keyword and table name are correct; only WHERE is incomplete.Final Answer:
Missing condition after WHERE clause. -> Option AQuick Check:
WHERE needs a condition [OK]
- Leaving WHERE empty
- Confusing missing SET with WHERE error
- Assuming query runs without WHERE condition
You have a table students with columns id, name, and grade. You want to increase the grade by 5 for all students who scored less than 50, but not exceed 50 after update. Which query correctly does this?
Solution
Step 1: Understand the requirement
Increase grade by 5 only for grades less than 50, but cap max grade at 50.Step 2: Analyze each option
UPDATE students SET grade = 50 WHERE grade < 50; sets all below 50 to 50 directly, no increase by 5. UPDATE students SET grade = grade + 5 WHERE grade < 50 AND grade + 5 <= 50; increases by 5 only if result ≤ 50, skipping some rows. UPDATE students SET grade = grade + 5 WHERE grade < 50; increases by 5 without capping at 50. UPDATE students SET grade = CASE WHEN grade + 5 > 50 THEN 50 ELSE grade + 5 END WHERE grade < 50; uses CASE to increase by 5 but caps at 50 correctly.Final Answer:
UPDATE students SET grade = CASE WHEN grade + 5 > 50 THEN 50 ELSE grade + 5 END WHERE grade < 50; -> Option AQuick Check:
Use CASE to cap values in UPDATE [OK]
- Not capping grade at 50
- Updating all rows without WHERE
- Skipping rows that would exceed 50
Ready to prove it?
Pass the test and this pattern moves unknown
