DELETE without WHERE (danger) in SQL - Time & Space 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.
Analyze the time complexity of the following SQL command.
DELETE FROM employees;
This command deletes all rows from the employees table without any condition.
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.
As the number of rows grows, the work to delete all rows grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 delete operations |
| 100 | 100 delete operations |
| 1000 | 1000 delete operations |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to delete all rows grows in direct proportion to how many rows there are.
[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.
Understanding how deleting all data scales helps you reason about database operations and their costs in real projects.
"What if we add a WHERE clause that matches only a few rows? How would the time complexity change?"