DELETE with WHERE clause in MySQL - Time & Space Complexity
When deleting rows from a database table using a condition, it's important to understand how the time taken grows as the table gets bigger.
We want to know how the number of rows affects the work the database does to delete matching rows.
Analyze the time complexity of the following code snippet.
DELETE FROM employees
WHERE department = 'Sales';
This code deletes all rows from the employees table where the department is 'Sales'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows to check the
departmentvalue. - How many times: Once for each row in the
employeestable.
As the number of rows in the table 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; double the rows means double the checks.
Time Complexity: O(n)
This means the time to delete rows grows in a straight line with the number of rows in the table.
[X] Wrong: "Deleting rows with a WHERE clause always takes the same time no matter how big the table is."
[OK] Correct: The database must check each row to see if it matches the condition, so more rows mean more work.
Understanding how deleting rows scales helps you explain database performance clearly and shows you know how data size affects operations.
"What if there was an index on the department column? How would the time complexity change?"
Practice
WHERE clause do in a DELETE statement in MySQL?Solution
Step 1: Understand the role of DELETE
The DELETE statement removes rows from a table.Step 2: Understand the role of WHERE
The WHERE clause filters which rows are affected by DELETE.Final Answer:
It limits which rows get deleted based on a condition. -> Option AQuick Check:
WHERE clause = filter rows to delete [OK]
- Thinking WHERE adds rows
- Confusing WHERE with table name
- Assuming DELETE changes table structure
age is less than 18 from a table named users?Solution
Step 1: Check DELETE syntax
The correct syntax starts with DELETE FROM followed by the table name.Step 2: Verify WHERE clause usage
WHERE clause uses condition like age < 18 to filter rows.Final Answer:
DELETE FROM users WHERE age < 18; -> Option BQuick Check:
Correct DELETE syntax = DELETE FROM users WHERE age < 18; [OK]
- Omitting FROM keyword
- Using DELETE * which is invalid
- Replacing WHERE with IF
employees with rows:{id:1, name:'Alice', dept:'HR'},{id:2, name:'Bob', dept:'IT'},{id:3, name:'Carol', dept:'HR'}What will be the remaining rows after running:
DELETE FROM employees WHERE dept = 'HR';Solution
Step 1: Identify rows matching WHERE condition
Rows with dept = 'HR' are Alice and Carol (id 1 and 3).Step 2: Delete matching rows and keep others
Only Bob (id 2) with dept 'IT' remains after deletion.Final Answer:
[{id:2, name:'Bob', dept:'IT'}] -> Option AQuick Check:
Deleted HR rows, kept IT row [OK]
- Deleting all rows by missing WHERE
- Keeping deleted rows by mistake
- Confusing which rows match condition
DELETE FROM orders WHERE;Solution
Step 1: Check syntax after WHERE
WHERE clause must have a condition to filter rows.Step 2: Identify missing condition error
Query ends with WHERE but no condition is given, causing syntax error.Final Answer:
Missing condition after WHERE clause. -> Option CQuick Check:
WHERE needs condition [OK]
- Leaving WHERE empty
- Thinking WHERE is optional but incomplete
- Confusing DELETE syntax with SELECT
products with columns id, name, and stock. You want to delete all products that have zero stock but keep products with NULL stock values. Which query correctly deletes only products with stock exactly zero?Solution
Step 1: Understand NULL behavior in WHERE
Comparisons with NULL using =, !=, <= return UNKNOWN (false); use IS NULL to check NULL.Step 2: Identify condition for stock exactly zero
stock = 0 matches only zero, excludes NULL and other values.Step 3: Analyze other options
stock <= 0 deletes zero and negatives (keeps NULL); stock IS NULL deletes NULL stocks; stock != NULL never matches (comparison with NULL always UNKNOWN).Final Answer:
DELETE FROM products WHERE stock = 0; -> Option DQuick Check:
Use = 0 to delete zero stock only [OK]
- Using <= 0 deletes negatives too
- Using != NULL deletes nothing
- Confusing NULL with zero
Ready to prove it?
Pass the test and this pattern moves unknown
