DELETE with WHERE condition in SQL - Time & Space Complexity
When we delete rows from a database table using a condition, the database must find which rows match before removing them.
We want to understand how the time to delete grows as the table gets bigger.
Analyze the time complexity of the following code snippet.
DELETE FROM employees
WHERE department = 'Sales';
This code deletes all employees who work in the Sales department.
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 table.
As the number of rows grows, the database must check more rows to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to delete grows in a straight line as the table gets bigger.
[X] Wrong: "Deleting with a WHERE condition is always very fast regardless of table size."
[OK] Correct: The database must check each row to see if it matches the condition, so bigger tables take more time.
Understanding how delete operations scale helps you explain database performance clearly and shows you know what happens behind the scenes.
"What if the department column has an index? How would the time complexity change?"
Practice
DELETE statement with a WHERE condition do in SQL?Solution
Step 1: Understand DELETE statement
The DELETE statement removes rows from a table.Step 2: Effect of WHERE condition
When a WHERE condition is present, only rows matching that condition are deleted.Final Answer:
Removes only the rows that match the condition specified in WHERE. -> Option AQuick Check:
DELETE with WHERE removes matching rows only [OK]
- Thinking DELETE always removes all rows
- Confusing DELETE with UPDATE
- Assuming WHERE creates a new table
age is greater than 30 from a table named employees?Solution
Step 1: Identify correct DELETE syntax
The correct syntax starts with DELETE FROM followed by the table name.Step 2: Check WHERE clause usage
The WHERE clause filters rows to delete; it must follow the table name.Final Answer:
DELETE FROM employees WHERE age > 30; -> Option CQuick Check:
DELETE FROM + table + WHERE condition [OK]
- Omitting FROM keyword
- Using REMOVE instead of DELETE
- Adding * after DELETE which is invalid
products with rows:{id:1, name:'Pen', stock:10}, {id:2, name:'Pencil', stock:0}, {id:3, name:'Eraser', stock:5}What will be the result of this query?
DELETE FROM products WHERE stock = 0;
Solution
Step 1: Identify rows matching condition
Only the product with stock = 0 is the one with id 2 (Pencil).Step 2: Effect of DELETE with WHERE
DELETE removes only rows matching the condition, so only id 2 is deleted.Final Answer:
Deletes the product with id 2 only. -> Option BQuick Check:
DELETE WHERE stock=0 removes matching rows only [OK]
- Assuming all rows are deleted
- Thinking condition is ignored
- Expecting syntax error for valid query
DELETE FROM orders WHERE;
Solution
Step 1: Check syntax after WHERE
WHERE clause must have a condition; empty WHERE is invalid syntax.Step 2: Result of missing condition
SQL parser throws syntax error because condition is missing.Final Answer:
Missing condition after WHERE causes syntax error. -> Option DQuick Check:
WHERE must have a condition, else syntax error [OK]
- Leaving WHERE empty thinking it deletes all
- Confusing DELETE with UPDATE
- Expecting query to run without error
users with columns id, name, and status. You want to delete all users whose status is either 'inactive' or 'banned'. Which query correctly deletes these users?Solution
Step 1: Understand condition for multiple values
To delete rows where status is 'inactive' or 'banned', use OR or IN clause.Step 2: Compare options
DELETE FROM users WHERE status = 'inactive' OR 'banned';-- OR 'banned' (literal string) evaluates to true, deleting all rows.DELETE FROM users WHERE status IN ('inactive', 'banned');-- correctly matches either value.DELETE FROM users WHERE status = 'inactive' AND status = 'banned';-- AND condition is impossible, deletes none.DELETE FROM users WHERE status = 'inactive', 'banned';-- invalid comma syntax.Final Answer:
DELETE FROM users WHERE status IN ('inactive', 'banned'); -> Option AQuick Check:
Use WHERE status IN (...) for multiple values [OK]
- Using AND instead of OR for multiple conditions
- Incorrect syntax with commas in WHERE
- Not using IN for multiple values
Ready to prove it?
Pass the test and this pattern moves unknown
