SQL DELETE: remove rows without removing too many

DELETE removes rows from a table: a dropped order item, cancelled orders, customer accounts that never bought anything. Once a row is deleted, it is gone, so the WHERE condition matters even more here than it does with UPDATE.

In short: DELETE FROM order_items WHERE order_id = 1017 AND product_id = 7; removes exactly one row. DELETE always removes whole rows, so no columns go between DELETE and FROM. Without WHERE, DELETE removes every row in the table.

Before you start: conditions with WHERE and changing data with UPDATE.

Try it now

Delete away: every run starts from fresh data. After Run, the result panel shows the rows your statement deleted.

Task Northline Printing is dropping the ink cartridges (product_id 6) from its open order 1018. Delete exactly that item from order_items and keep the rest. After Run or Check you will see the rows that were deleted.

order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.9

How DELETE works

DELETE FROM names the table and WHERE the rows. The database tests every row and removes each one the condition is true for. As with UPDATE, look at those rows with a SELECT first.

SELECT *
FROM order_items
WHERE order_id = 1017;
order_idproduct_idquantityunit_price
1017233.49
1017726.99

Order 1017 has two items. To remove only the sticky notes (product_id 7), the condition needs both columns: DELETE FROM order_items WHERE order_id = 1017 AND product_id = 7;. Afterwards just the pens are left. With WHERE order_id = 1017 on its own, both rows would be gone.

Deleting with a subquery

Often the rule lives in another table, such as "customers without orders". Then WHERE tests against a subquery with IN, NOT IN or EXISTS. Every database understands this form.

Task Time to tidy up the customer file: customer accounts that have never placed a single order should go. Delete from customers every customer whose customer_id does not appear anywhere in orders.

customers 14 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164
20Seidel Engineering LtdBristolUK2024-05-06Business0117 496 0555
21Paul LambertDenverUSA2025-02-17ConsumerNULL
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Foreign keys: when the database says no

Orders point to customers through customer_id, and order items point to orders through order_id. These references are foreign keys, and the database keeps them valid. Try DELETE FROM customers WHERE customer_id = 11 and the statement fails: Daniel Kim has order 1016, which would otherwise point to a customer who no longer exists.

The fix is the right order: first the rows that point (children), then the row they point to (parent). A foreign key can also be set up to delete dependent rows automatically (ON DELETE CASCADE); that is covered in primary and foreign keys.

Task Cancelled orders should disappear from the system completely: the orders themselves and their items. Delete every order with the status cancelled from orders, and every item of those orders from order_items. After Run or Check you will see which rows were deleted from which table.

orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL
order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.9

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Common mistakes

Forgetting WHERE

DELETE FROM order_items; removes all 38 items without asking. In the first exercise the check says "After your changes: The check shows 38 rows" when a single one is wanted, and the result panel lists all 38 deleted rows. On tables that others point to, like customers, a foreign key often stops you. Don't count on it.

Deleting the parent before the children

Delete the orders before their items and you get a foreign key error. The exercise shows "Other rows still point to this row (foreign key)." Rows in another table still point to the row you are trying to delete. Delete those first.

OR instead of AND

WHERE order_id = 1018 OR product_id = 6 deletes every item on order 1018 plus the ink cartridges from every other order. The check reports too many rows. For "exactly this one" you need AND.

Columns after DELETE

For DELETE * FROM order_items … the exercise reports a syntax error near "*". Nothing goes between DELETE and FROM, because DELETE always removes the entire row. To clear a single value, use UPDATE … SET column = NULL instead.

TRUNCATE vs DELETE

To empty a whole table, many databases offer TRUNCATE TABLE. It is faster than DELETE without WHERE but behaves differently from one database to the next. SQLite, and so our exercises, has no TRUNCATE, and the exercise rejects the statement. Write DELETE FROM table without WHERE instead.

DatabaseTRUNCATEDELETE based on another table
MySQLCounts as DDL, causes an implicit commit and cannot be rolled back. Fails on InnoDB if foreign keys in other tables reference the table. Resets AUTO_INCREMENT.DELETE t1 FROM t1 LEFT JOIN t2 ON … WHERE t2.id IS NULL
PostgreSQLCan be rolled back inside a transaction. With foreign keys, only if the referencing tables are truncated in the same command or with CASCADE. Resets sequences only with RESTART IDENTITY.DELETE FROM t1 USING t2 WHERE …
SQL ServerCan be rolled back inside a transaction. Not allowed on a table referenced by a foreign key. Resets IDENTITY to its seed.DELETE t1 FROM t1 JOIN t2 ON … WHERE …
SQLite (these exercises)Does not exist. DELETE FROM table without WHERE is sped up internally (the "truncate optimization").A subquery with IN or EXISTS

One more difference: in SQLite, foreign keys are switched off by default and have to be turned on for each connection with PRAGMA foreign_keys = ON. In our exercises they are on.

Soft delete: flag instead of delete

Many applications never actually delete records. They mark them instead, for example with a deleted_at column. An UPDATE replaces the DELETE, and every query filters with WHERE deleted_at IS NULL. The upside: invoices and reports stay complete, and a mistake can be undone. The downside: every query has to remember the filter, and data that really has to go is still there.

FAQ

What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes rows, optionally only some of them. TRUNCATE empties the whole table. DROP TABLE removes the table itself, structure and all. After DELETE or TRUNCATE the table still exists, just empty.

Can I undo a DELETE?

Inside a transaction, with ROLLBACK before COMMIT; otherwise only from a backup. See transactions.

Do new rows reuse the IDs of deleted ones?

In SQLite without AUTOINCREMENT they can, if the row with the highest ID was deleted, since the next ID is usually the highest plus one. SQL Server does not hand out IDENTITY values again.

How do I delete duplicate rows?

First find the duplicates (finding duplicates), then use DELETE with a subquery to remove all but one row per group, for example keeping the one with the lowest ID.

How do I delete rows using a join?

The portable way is a subquery: WHERE id IN (SELECT …) or WHERE EXISTS (…). The join syntax differs by database, as the table above shows.

Next up: CREATE TABLE

You can add, change and remove rows. Now build your own tables: columns, data types, required fields and the primary key that hands out the automatic IDs.

Practice CREATE TABLE

Write to us

Choose language

Select your preferred language