
A reading app builds a reminder list of sections that were opened but have no recorded completion by that reader. An old unfinished session should not survive once a later session finishes. The table records a reading session, not the current state of a book. One reader may revisit several sections. Finished marks this session’s section completion and does not imply that the whole book was finished.
The rows behind the report
The fixture uses a fresh SQLite database. Its small size allows the result to be checked by hand before the query is applied to an export with thousands of rows. The unit of observation matters here because a query can execute cleanly and still answer a different editorial or operational question.
CREATE TABLE reads (
id INTEGER PRIMARY KEY,
reader TEXT,
book TEXT,
section INTEGER,
seconds INTEGER,
finished INTEGER
);
INSERT INTO reads VALUES
(1, 'Mira', 'Atlas', 1, 60, 1),
(2, 'Mira', 'Atlas', 2, 130, 1),
(3, 'Ira', 'Atlas', 1, 95, 1),
(4, 'Ira', 'Orbit', 1, 22, 0),
(5, 'Mira', 'Orbit', 1, 75, 1),
(6, 'Dev', 'Orbit', 2, NULL, 0);What the query returns
SELECT reader,book,section
FROM reads EXCEPT SELECT reader,book,section
FROM reads
WHERE finished=1
ORDER BY 1,2,3;| reader | book | section |
|---|---|---|
| Dev | Orbit | 2 |
| Ira | Orbit | 1 |
EXCEPT compares the three projected columns as a set. Including session IDs would prevent a later completion from cancelling the earlier open.
What changes with one more record?
Append this single row to the same fixture and rerun the query. Its named fields are id = 7, reader = Dev, book = Atlas, section = 1, seconds = 90, finished = 1. Keep the other rows intact so a comparison isolates the effect of this one insert.
INSERT INTO reads VALUES (7, 'Dev', 'Atlas', 1, 90, 1);The original query returned 2 rows. After the insert it returns 2. Every output row remains the same. The extra record is a useful negative test because adding data to the table has not changed the reported observation. 2 earlier output rows still appear unchanged.
Before exporting the result, document what one displayed row represents and which fields decide its order. Look for the same shape of input in an anonymized sample from the application. A query validated only against an empty development database cannot show what happens when a record is missing, repeated or tied with another record.
SQLite reference for EXCEPT · SQLite SQL syntax reference
Sources and further reading
Cover photograph: HuaweiRH2288HV2 by Open Grid Scheduler / Grid Engine. CC0. Cropped and typeset for this cover. The photograph illustrates the topic.
