Delete selected rows
Your task
Delete receipt 4 and return the remaining receipt count.
DELETE removes rows that match a condition. Here changes take place in a temporary exercise database.
Sample data
receipts| id | sold_on | amount | channel | metadata |
|---|---|---|---|---|
| 1 | 2025-01-01 | 36 | ONLINE | {"currency":"EUR","tags":["gift","jazz"]} |
| 2 | 2025-01-16 | 22 | Store | {"currency":"EUR","tags":["indie"]} |
| 3 | 2025-01-31 | 54 | online | {"currency":"EUR","tags":["returning","bundle"]} |
| 4 | 2025-03-04 | 38 | STORE | {"currency":"EUR","tags":[]} |
| 5 | 2025-04-09 | 44 | Online | {"currency":"EUR","tags":["indie"]} |
Expected output
| COUNT(*) |
|---|
| 4 |
Show a hint
Use DELETE FROM receipts WHERE id=4, followed by COUNT(*).
Writing and modeling data guide
Define the shape of a table
A primary key identifies a record. NOT NULL makes a value mandatory, and CHECK restricts allowed values. Put these rules in the table definition so SQLite enforces them on writes. The PRAGMA query below inspects the column declarations.
CREATE TABLE deliveries (
id INTEGER PRIMARY KEY,
album_id INTEGER NOT NULL REFERENCES albums(id),
copies INTEGER NOT NULL CHECK (copies > 0)
);
PRAGMA table_info(deliveries);Preview the rows before changing them
In an update or delete, WHERE determines which rows change. Try the same condition in a SELECT first. The write exercises start from a fresh practice database on each run, so include all the needed statements in one submission.
SELECT id, title, stock FROM albums WHERE id = 3;
UPDATE albums SET stock = stock + 5 WHERE id = 3;
SELECT id, title, stock FROM albums WHERE id = 3;Group changes into a transaction
BEGIN starts a transaction. COMMIT keeps its changes and ROLLBACK undoes them. Here the final SELECT shows the original stock because the UPDATE was rolled back.
BEGIN;
UPDATE albums SET stock = 0 WHERE id = 1;
ROLLBACK;
SELECT title, stock FROM albums WHERE id = 1;Worked solution
DELETE FROM receipts WHERE id=4; SELECT COUNT(*) FROM receipts;Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.
Results
Loading SQLite…
