Result multiset tests
Overview
When a query has no required order, compare rows with their multiplicities, not just their distinct values. A result containing two copies of A and one NULL differs from one copy of A and one NULL. If the contract requires order, compare the complete sequence.
First example
SELECT label,COUNT(*) FROM pt_item GROUP BY label ORDER BY label;Observed SQLite result (rows in JSON):
[ [ null, 1 ], [ "", 1 ], [ "A", 2 ]]How it works
Group each result by all projected columns and count occurrences. NULL groups together in GROUP BY; a NULL-safe comparison is required when matching groups. On the host, use a Counter of typed row tuples for unordered equality; check column names, types and precision separately. Never stringify NULL into the text NULL.
Practical example
SELECT COUNT(*),COUNT(DISTINCT label),COUNT(label) FROM pt_item;Observed SQLite result (rows in JSON):
[ [ 4, 2, 3 ]]Practice and mistakes
Practice: compare the expected result with a candidate that drops duplicates. Hint 1: compare counts by label. Hint 2: include the NULL group and distinguish it from the empty string. Mistake: checking only total row count or set equality.
PostgreSQL 17 example — NOT RUN
SELECT * FROM (VALUES ('A'),('A'),(NULL)) AS x(v) EXCEPT ALL SELECT * FROM (VALUES ('A'),(NULL)) AS y(v);Expected result, not executed: "One A remains."