Lesson 8.4
Deleting Rows Safely with SQL DELETE
DELETE removes rows for good. The ritual: SELECT the rows first, check them, then run the delete.
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 aDELETE. 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 ShopTable · orders6 columns · 24 rows
Table · orders
6 columns · 24 rowsOne row per order. Unshipped orders have a null shipped_date.
order_id intcustomer_id intorder_date dateshipped_date datestatus textcoupon_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, and confirm the purge by selectingcount(*) of the remaining cancelled orders ascancelled_remaining. It should be 0.
SQL Workbench
⌘↵ to run·
Expected answer
- Columns: cancelled_remaining.
- Rows: 1, a single count.
- After deleting the cancelled orders, the count of cancelled orders is 0.