r/learnSQL • • 4d ago

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.

5 Upvotes

7 comments sorted by

6

u/Creepy_Purchase_501 4d ago

Looks like it just assumes your database has minimal quality standards in place.

I would too, honestly, if I didn’t know ahead of time I was dealing with something so deranged.

4

u/Imaginary__Bar 4d ago

Exactly. "Trap 2" isn't wrong. It's the correct answer from the data given.

4

u/Creepy_Purchase_501 4d ago

None of it is wrong if you make the banal assumption that whoever set up the db bothered to do the bare minimum and added a foreign key and uniqueness constraints.

1

u/landry_kb 3d ago

That assumption is the trap. The real data this came from had no such guarantees. 99 sales returned more than once, and rows for sales that never happened. The models that checked passed. The ones that assumed didn't. And in real life data is always messy anyways.

3

u/Creepy_Purchase_501 3d ago

The assumption holds in the real world 99.9% of the time. Can you imagine a company whose software would allow you to create a return for a sale that did not exist? The’d be defrauded in a minute. There’s also usually some sort of hygiene standard where duplicates will only be allowed to exist in the raw transactions table, but will be taken care of by the time data gets into a table someone bothered to call returns. Otherwise, why would it exist?

There’s messy and there’s incompetent. Messy happens all the time, but incompetent is too dangerous for a company to exist.

1

u/T1lted4lif3 3d ago

but that just sounds like human error, can't really fault an AI for this, if you made it and told an engineer the database is legit, they would assume so as well no? Because otherwise everything would fall apart

1

u/UnderstandingFun9659 3d ago

A developer should understand data structure, relationships between data, integrity constraints and data and data quality before writing code.

If I were a developer, I would ask,

  1. Why do you have sale ids in returns table that don’t exist in sales table?
  2. What do the amounts in sales table represen? Total amount of sales?
  3. There are multiple rows for a sales id in returns table. assuming that a sales id had multiple items. If items from same sale were returned either at one time or multiple different time periods, how do you tell total value of all items returned as there is no item level information in the above data?

I worked as a database developer for 16 years. I took a break for family and can’t get back due to break.