Skip to content
/ Chapter 8 · Changing Data and Tables
Lesson 8.4 · garden_shop
Lesson 8.4

Deleting Rows Safely with SQL DELETE

DELETE removes rows from a table. Preview the target rows first, and use a transaction when you need a rollback path.

DELETE removes rows, and like UPDATE it obeys your WHERE clause, or deletes everything if you forget it.

The safe ritual is two steps: first SELECT the rows your filter matches and eyeball them, then run the same filter as a DELETE. The starter below is that first step. Run it before you change anything.

Pattern
-- 1. measure
select * from orders where status = 'cancelled';

-- 2. cut
delete from orders where status = 'cancelled';
Schema · Garden Shop Table · orders 6 columns · 24 rows
Table · orders

One row per order. Unshipped orders have a null shipped_date.

6 columns · 24 rows
order_id int customer_id int order_date date shipped_date date status text coupon_code text
Your task

The shop purges cancelled orders. First run the starter to see which orders are 'cancelled'. Then delete every order with that status. Confirm the purge touched only those orders: return each status with its count(*) as orders, sorted by status.

·

Run your query to inspect the result.

Expected answer
  • Columns: status, orders.
  • Rows: 3 statuses, sorted by status. cancelled no longer appears.
  • pending 3, processing 3, shipped 17: 23 orders, one fewer than before. A DELETE with no WHERE returns no rows at all.