r/SQL • • 18h ago

SQLite A 60-second SQL test with three traps. Most AIs I've graded fall for at least one

I grade AI models on data questions for a living experiment, and the same three mistakes keep showing up. Here's a pocket version: paste it into whatever AI you use and see how it does. The database is SQLite.
sales (sale_id, amount):
1: 412.75 · 2: 268.40 · 3: 529.99 · 4: 145.20 · 5: 876.10 · 6: 233.65
7: 694.30 · 8: 358.55 · 9: 921.00 · 10: 187.45 · 11: 543.80 · 12: 476.25
returns (sale_id, one row per return event, 25 rows):
2, 5, 9, 2, 7, 3, 2, 11, 5, 12, 9, 4, 2, 7, 10, 5, 1, 9, 12, 4, 11, 8, 10, 13, 13
Task: write SQLite SQL returning exactly two columns: returned_sales (how many sales came back) and total_returned_sales (their total value, each sale counted once), and state the two numbers.
Answers, so you can grade it yourself:
Pass: 11 and 5,413.79, exactly two columns, SQL SQLite can run.
Trap 1: a total of 11,860.19 means it joined and summed, so sale 2 was counted 4 times. Counted twice (or four times).
Trap 2: a count of 12 means it counted sale 13, which is in returns but was never sold.
Trap 3: FETCH FIRST, EXTRACT, or more than two columns: wrong database or didn't follow the order.
On the full-size version of this case (500 return rows, 99 sales returned more than once), the counted-twice error invents $31,490.03. I've watched a model miss it twice in graded matches.
Curious what yours answered. Post the number it gave.

2 Upvotes

13 comments sorted by

2

u/JaceBearelen 18h ago

Grok 4.7 in cursor got it right first try.

0

u/landry_kb 17h ago

A pass counts too. How did it count each sale once, DISTINCT or IN?

2

u/JaceBearelen 17h ago

Here's the whole reply excluding the thinking step:

returned_sales = 11, total_returned_sales = 5413.79

SELECT

COUNT(*) AS returned_sales,

ROUND(SUM(amount), 2) AS total_returned_sales

FROM sales

WHERE sale_id IN (SELECT sale_id FROM returns);

returns lists 25 events, but several sales appear more than once (sale 2 four times, sales 5 and 9 three times, and so on). The subquery only needs to mark which sale IDs came back; the outer query then reads each matching row from sales once, so the amount is not added again for every return event.

Sale 6 never appears in returns. Sale 13 appears twice there but has no row in sales, so it is left out of both the count and the total. The 11 sales that did come back are 1–5 and 7–12.

0

u/landry_kb 17h ago

Wow great it caught sales 13 without being asked and with the proper reasoning. Interesting!
Thanks for the feedback!

1

u/JaceBearelen 17h ago

Yeah this kinda stuff is how I know my job security is completely gone. At my org we give it some skills to push it towards investigating the data, lots of metadata, and a feedback loop so it can run queries and analyze output. It’s damn near infallible on our data.

0

u/landry_kb 16h ago

I see. So it gets infaillible because it checks and get to know the data instead of guessing. Can it guess and identify similar patterns and traps in complex data it doesn't know yet?

6

u/aaahhhhhhfine 15h ago

Maybe it's an odd question here, but why can sales be returned more than once? It seems like the "trap" here relates to an assumption about the db structure that isn't explicit in the model. If there were relationships in the model then it would be clear that returns could happen more than once. In that case, I suspect most AIs would get this correct - even old ones. I suspect that it's just that, in the absence of a relationship, it's (not totally unreasonably) assuming that the relationship between sales and returns is one to one.

4

u/TempMobileD 12h ago

I thought the same. The correct interpretation of this data isn’t clear. What if someone is receipt scamming and actually just returns a thing multiple times. You’d want to count that.
Saying there’s one row per return and then immediately presenting multiple rows per return invalidates the whole premise that this is a usable dataset IMO.

Also who cares if 13 was never sold? Again, this shouldn’t just be discarded it should be investigated, in an actual dataset you’d want a logical explanation before just throwing this out. Is data missing? It’s the highest number so has it just not come in yet in the sales table? Why are they out of sync if that’s the case? Is there a date range somewhere in the underlying logic? Maybe 13 was for a free coffee, and the sales logic doesn’t like £0 transactions? I don’t think it’s appropriate to just present a solution where an assumption has been made that it’s invalid.

2

u/Chainwreck 15h ago

One real life scenario that doesn’t up in this example is that a return claim can be denied and then appealed and accepted the next time. That or if you are looking at oracle transactional tables there are massive amounts of duplicates for rows and states and sometime you have to merge or back fill null values of the rows together to get a complete picture of a particular version of an order or fulfillment.

5

u/aaahhhhhhfine 15h ago

Sure... Yeah I mean I get there are reasons where you could have this.

I guess my point is more that, in a database that was better specified you would expect to see a relationship that makes this reality clear. In the absence of that relationship it's not all that weird to see the models guessing that the relationship is one to one.

1

u/NervouslyTurbulent 18h ago

It's always the duplicate returns that get people. Saw a junior on my team do exactly this last month and couldn't understand why their revenue report looked like we'd had the best week in company history.

My local instance got 11 and 5,413.79 but only after I explicitly told it to use DISTINCT in the join. First pass it spat out the inflated number and looked real confident about it.

1

u/landry_kb 18h ago

That is the trap!! Which local model was it on your local instance it's okay to share?

1

u/BigMikeInAustin 33m ago

It can't do it until it ingests enough text of the problem. Now you've gone and taught the next LLM version that reads this!