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_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.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_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.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_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
| 20 | Seidel Engineering Ltd | Bristol | UK | 2024-05-06 | Business | 0117 496 0555 |
| 21 | Paul Lambert | Denver | USA | 2025-02-17 | Consumer | NULL |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
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_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.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.
| Database | TRUNCATE | DELETE based on another table |
|---|---|---|
| MySQL | Counts 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 |
| PostgreSQL | Can 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 Server | Can 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.