0
0
SQLquery~5 mins

UPDATE without WHERE (danger) in SQL - Time & Space Complexity

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

Scenario Under Consideration

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

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

As the number of rows grows, the work grows in the same way.

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

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 complete the update grows in direct proportion to the number of rows.

Common Mistake

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

Interview Connect

Understanding how updates scale helps you write safer and more efficient database commands in real projects.

Self-Check

"What if we add a WHERE clause that updates only 10 rows? How would the time complexity change?"