CodeBlanks Logo Icon CodeBlanks

Conditional DELETE

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

query.sql
-- delete zero stock
DELETE FROM products
WHERE

Sample solution

query.sql
DELETE FROM products
WHERE stock = 0

Expected 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

Question
DELETE FROM products
WHERE ____;
Answer
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

All lessons in Beginner