0
0
SQLquery~5 mins

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

Choose your learning style9 modes available
Time Complexity: DELETE without WHERE (danger)
O(n)
Understanding Time Complexity

When deleting data from a database, it is important to understand how the time taken grows as the data size increases.

We want to know how the cost changes when deleting all rows without any filter.

Scenario Under Consideration

Analyze the time complexity of the following SQL command.


DELETE FROM employees;
    

This command deletes all rows from the employees table without any condition.

Identify Repeating Operations

Look for repeated actions that affect performance.

  • Primary operation: Scanning and deleting each row in the table.
  • How many times: Once for every row in the table.
How Execution Grows With Input

As the number of rows grows, the work to delete all rows grows too.

Input Size (n)Approx. Operations
1010 delete operations
100100 delete operations
10001000 delete operations

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 delete all rows grows in direct proportion to how many rows there are.

Common Mistake

[X] Wrong: "Deleting all rows is instant because there is no WHERE clause."

[OK] Correct: Even without a filter, the database must visit and remove every row, so the time depends on the total rows.

Interview Connect

Understanding how deleting all data scales helps you reason about database operations and their costs in real projects.

Self-Check

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