UPDATE single column in SQL - Time & Space Complexity
When we update a single column in a database table, it is important to understand how the time taken changes as the table grows.
We want to know how the number of rows affects the time it takes to perform the update.
Analyze the time complexity of the following SQL update statement.
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
This code increases the salary by 5% for all employees in the Sales department.
Look for repeated actions that affect time.
- Primary operation: Checking each row to see if it belongs to the Sales department.
- How many times: Once for every row in the employees table.
As the number of employees grows, the database must check more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations 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: "Updating one column is always very fast and does not depend on table size."
[OK] Correct: Even if only one column changes, the database must still check each row to find which ones to update, so time grows with table size.
Understanding how updates scale helps you explain database behavior clearly and shows you know what affects performance in real projects.
"What if we add an index on the department column? How would the time complexity change?"