Conditional DELETE
- SQL course
- Beginner
- Lesson 84/115
- 4 min
Make DELETE conditional with WHERE.
Delete with WHERE
With WHERE you can delete many rows matching a condition. Write the condition carefully.
DELETE FROM products
WHERE stock = 0;This query removes all out-of-stock products.
Rows are deleted from products. Only the rows where stock = 0 holds are affected.
Delete with WHERE — details
WHERE can delete many rows. A wrong condition causes unwanted loss; check with SELECT first.
Practice Delete with WHERE
Delete from products where stock is 0.
Starter code
-- delete zero stock
DELETE FROM products
WHERESample solution
DELETE FROM products
WHERE stock = 0Expected output
2 satır silindi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Conditional delete
DELETE FROM products
WHERE ____;
DELETE FROM products
WHERE stock = 0;
Explanation: stock = 0 is the right answer.
2 What does DELETE FROM t WHERE stock = 0 do?
- Deletes all rows
- Nothing
- Deletes the rows with stock 0 Correct answer
- Increases the stock
Explanation: matching rows are deleted.
3 Conditional DELETE: match the terms
| Term | Meaning |
|---|---|
DELETE |
delete |
FROM |
table |
WHERE |
condition |
= |
equal |