0
0
SQLquery~5 mins

UPDATE with WHERE condition in SQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: UPDATE with WHERE condition
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of employees grows, the database checks more rows to find those in Sales.

Input Size (n)Approx. Operations
1010 checks
100100 checks
10001000 checks

Pattern observation: The work grows directly with the number of rows.

Final Time Complexity

Time Complexity: O(n)

This means the time to update grows in a straight line as the table gets bigger.

Common Mistake

[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.

Interview Connect

Understanding how updates scale helps you explain database behavior clearly and shows you know what happens behind the scenes.

Self-Check

"What if we add an index on the department column? How would the time complexity change?"