UPDATE without WHERE (danger) in SQL - Time & Space Complexity
When we run an UPDATE command without a WHERE clause, it changes every row in the table.
We want to understand how the time it takes grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.05;
This code increases the salary by 5% for every employee in the table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database updates each row one by one.
- How many times: Once for every row in the table.
As the number of rows grows, the work grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to complete the update grows in direct proportion to the number of rows.
[X] Wrong: "Updating without WHERE is fast because it's just one command."
[OK] Correct: Even though it's one command, the database must still update every row, so the time grows with the table size.
Understanding how updates scale helps you write safer and more efficient database commands in real projects.
"What if we add a WHERE clause that updates only 10 rows? How would the time complexity change?"