0
0
SQLquery~5 mins

UPDATE single column in SQL - Time & Space Complexity

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

Scenario Under Consideration

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.

Identify Repeating Operations

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

As the number of employees grows, the database must check more rows.

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

Pattern observation: The number of operations 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: "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.

Interview Connect

Understanding how updates scale helps you explain database behavior clearly and shows you know what affects performance in real projects.

Self-Check

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