r/SQL • u/Plane_Big_5912 • 1d 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)?
4
u/UnluckyManner9152 1d ago edited 1d 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.
2
u/Hour-Measurement-835 1d ago
Ties in a window ORDER BY. Something like "latest deposit per user" written as
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC) = 1, with two deposits on the same timestamp in your data. Which of the two gets rank 1 is up to the engine, and I wouldn't count on DuckDB picking the same one every run once it's spreading the work over threads.That breaks your checker in a nasty way, because the learner's query passes on some runs and fails on others. When it misses, a single edit can still land on the expected rows by luck on that run, and the panel ends up naming a join or a COUNT that was never the problem. LIMIT 1 on an ORDER BY that isn't unique does the same thing.
The cheap guard I'd add is to run their query, before trying any edits, two or three times against copies of the tables rebuilt in shuffled order (
CREATE TABLE ... AS SELECT * FROM t ORDER BY random()). If the outputs disagree, tell them the result depends on row order and they need a tiebreaker, and skip the edit search for that query.1
u/Discordbothost 1d 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 1d ago
NOT IN against a subquery that has a NULL in it.
1
u/michallandry62 1d 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.