Checking a Query Against a Known Small Data Set
A small fixture with controlled duplicates and missing relationships can expose incorrect filters, ordering, or grouping.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Choose contrasting rows
Use one category with two matches, one with none, and one with only filtered-out matches. Predict all output rows before running the query. This tests the left join condition placement.
Assert grain and order
Compare meaningful IDs, result counts, and stable order under the contract. Test inputs that separate answers help make each way the query might fail observable.
WITH categories (id) AS (VALUES (1), (2), (3)),
articles (id, category_id, published) AS (
VALUES (11, 1, TRUE), (12, 1, TRUE), (13, 3, FALSE)
)
SELECT categories.id, COUNT(articles.id) AS published_count
FROM categories
LEFT JOIN articles ON articles.category_id = categories.id
AND articles.published = TRUE
GROUP BY categories.id
ORDER BY categories.id
The ordered result is (1, 2), (2, 0), (3, 0). Moving the publication test to WHERE removes categories 2 and 3. Counting every joined row with COUNT(*) makes each of those categories look as though it has an article.[1]
Build a tiny fixture that makes a join defect visible
For a book report, choose one author with two books and one author with none. Write the expected two counts by hand. A query returning only rows for the first author exposed an inner-join mistake if every author should appear. See left-join preservation.
Author A → books 7 and 9 → count 2
Author B → no books → count 0
Expected output includes both authors
Use local fixture data the query cannot reinterpret by accident
Distinct names and unequal numeric values help show swapped roles. Include a filter boundary and verify exact rows and order if order is part of the contract.