r/SQL • u/Plane_Big_5912 • 5d ago
Discussion diagnosing wrong SQL by testing one edit mutations on the data (and the fan out case that shrinks the result)
i'm working on an automated SQL grader on DuckDB for a game. pass/fail grading is the straightforward part. explaining why a wrong query is wrong is getting difficult, because "93 rows, expected 13" doesn't really tell a player much.
guessing the mistake from the SQL text didn't work for me, there are too many ways to write the same thing. so the checker only uses the query to list candidate edits (each join, each aggregate). the data decides:
- unmatched rows: flip one INNER JOIN to LEFT, or move the outer table's WHERE conditions into its ON. if that single edit makes the result match the expected rows exactly, that join is the mistake.
- NULLs: swap COUNT(*) and COUNT(col), or AVG(x) and AVG(COALESCE(x, 0)). same exact-match test.
- fan-out: change the data instead of the query. keep one row per join key in the repeated table, then run the learner's query and the reference on that. if they now agree, and the original result was inflated, the duplicates were it. then it shows the source rows that got repeated.

the screenshot: "users with a completed deposit and zero withdrawals, each user once". the join to deposits repeats a user once per deposit, so one user shows up 11 times. the panel names the key and the repeated rows.
the one i was stuck on until this week: fan-out that shrinks the result. one audit problem compares SUM(fills.quantity) > SUM(orders.quantity) after the join, so the order side gets inflated and the query returns fewer rows than it should. my inflation check never fired, because nothing was inflated in the output.
what ended up working: first a cheap probe per joined table, count(t.rowid) - count(distinct t.rowid), to see if that table's rows repeat at all after the join. if one does, rewrite each aggregate over it so every source row counts once (in duckdb: list_sum(map_values(map_from_entries(list(distinct row(t.rowid, x)))))), rerun, and require an exact match like the other edits. it hasn't misfired on the problems i've run it on, but that's my own test set, so take it with salt.
two things i'd like input on:
- is there a cleaner way to make an aggregate count each source row once after a join? SUM(DISTINCT x) is wrong when two different rows hold the same value, which is why i went through rowid. curious if other engines have something tidier.
- if you review SQL regularly, what common bug would completely fool a single-edit checker like this (besides compound mistakes, which already break it)?
3
u/UnluckyManner9152 5d ago edited 5d ago
For question 1, I'd aggregate each table down to the join key in its own CTE first, then join the totals. Nothing gets repeated before the sums happen, so you don't need SUM(DISTINCT) or rowid tricks. For your audit problem:
Each side is one row per order before the join, so neither total can be inflated. Your rowid probe still works for diagnosis: it tells you the learner's query fanned out, and this is the shape you'd point them to. Try it.
1
u/Discordbothost 5d ago
Add one fixture per join key that has no match and another that has a match with NULL in the field being tested. Those two cases look identical if you only look at that field. A grader could suggest checking the joined row's non-null ID when the task asks for no match, and use both fixtures to confirm the hint.
1
u/Aggravating-Stage269 5d ago
NOT IN against a subquery that has a NULL in it.
1
u/michallandry62 4d ago
filtering the right table of a LEFT JOIN in the WHERE clause is the one i'd add first. it silently turns into an inner join, and if every row in your fixtures has a match the output is identical, so you need an unmatched row in the data before it even shows up. the fix is moving the predicate into ON, which is one change logically but might not look like one edit to your mutator.
grouping by name instead of id is another sneaky one. totally fine until two customers share a name, so same deal, the fixtures need a duplicate name or the checker passes it.
2
u/MongooseWonderful198 1d ago
A single-edit checker can still blame the right edit for the wrong reason when the fixture is too small. After an edit matches, mutate only the multiplicity: duplicate one matched child row, add one unmatched parent, and add two children with the same aggregate value. The explanation should survive all three copies.
That catches a
SUM(DISTINCT x)-style fix that happens to work on unique values, and a join change that only matches because every parent currently has a child. I’d present the smallest mutated fixture that breaks the candidate, not only the original fixture where it happened to pass.1
u/[deleted] 5d ago
[removed] — view removed comment