Editor

Delete the items of cancelled orders

Medium · Online shop database · DELETE, Subqueries

The shop wants to clear out the items of cancelled orders. Delete from order_items every row that belongs to an order with status cancelled.

Keep the orders themselves, and keep the items of delivered and shipped orders.

After your SQL, this query shows

SELECT * FROM order_items ORDER BY id;
idorder_idproduct_idquantityunit_price
111122.99
21933.99
3241189
4216215.99
532189.5
8515227.5
9516115.99
1065145
1166218.5
12713154.99
13712412
1483239
16101224.99
1710856.49
1811171119
19129103.99
2012826.49
211310120
221312212
24156118.5
251614139
261613154.99
28183139
291915127.5
301916215.99
31205248
322112312
332241199
3423836.49
352414139

First 30 of 35 rows.

The online shop tables

customers: 18 rows, first 5 shown

idnamecitycountrysignup_datephone
1Ravi KumarBengaluruIndia2025-01-12+91 98450 12345
2Emily JohnsonAustinUSA2025-01-25+1 512 555 0142
3Hiroshi SatoOsakaJapan2025-02-08NULL
4Ana SouzaSao PauloBrazil2025-02-19+55 11 91234 5678
5Priya SharmaMumbaiIndia2025-03-03+91 99200 54321

order_items: 40 rows, first 5 shown

idorder_idproduct_idquantityunit_price
111122.99
21933.99
3241189
4216215.99
532189.5

orders: 28 rows, first 5 shown

idcustomer_idorder_datestatus
112025-02-03delivered
222025-02-14delivered
332025-03-01delivered
412025-03-22cancelled
542025-04-05delivered

products: 18 rows, first 5 shown

idnamecategorypricestock
1Wireless MouseElectronics24.99120
2Mechanical KeyboardElectronics89.545
3USB-C HubElectronics3980
4Noise Cancelling HeadphonesElectronics19925
5Intro to StatisticsBooks4860
-- Write your SQL here