r/learnSQL • • Sep 05 '26

The failure mode of AI-generated SQL isn't syntax errors. It's queries that run clean and return wrong numbers.

36 Upvotes

21 comments sorted by

10

u/NW1969 Sep 05 '26

My first sanity check when I’m reviewing AI-generated queries is exactly the same as my first sanity check when I’m reviewing non AI-generated queries.

I’m not sure why anyone would think the process is any different depending on who/what generated the query

3

u/uncertain_expert Sep 05 '26

Exactly- this result could be expected from a junior SQL writer with or without the use of AI in writing the query. OP’s review process is flawed.

1

u/Samantha_2024 Sep 06 '26

Exactly. If the query is wrong, it’s wrong. Doesn’t really matter whether a junior dev or an LLM wrote it.

6

u/BoSt0nov Sep 05 '26

~40% off is ”in the ball park” with this type of precision I would think no one at the company has a clue whats going on… How do you not have any historical data to verify against. As bad as that mistake is, I think the junior isnt the only one to blame here.

2

u/Qphth0 Sep 05 '26

My first thought as well.

2

u/IAmQueeg500 Sep 06 '26

Why bother testing with a known data set if it feels like it could be working

4

u/bitterjack Sep 05 '26

God. Why did you wrote this post in AI? You're part of the problem.

2

u/Key_Ingenuity5340 Sep 05 '26

Where was this done? In a report or in a pipeline?

2

u/DMReader Sep 05 '26

The better move is to have ai write tests when you do updates. I’ve found that helps cut down on errors.

1

u/taglius Sep 05 '26

I don’t let AI write SQL for this reason - we have had bad results way more often than not. Build UIs, ETL pipelines, web services? Knock yourself out. Not SQL

1

u/i_literally_died Sep 05 '26

I don't understand how people can blindly mash AI SQL into their systems. A lot of code is generic - you generally know how Python, Java, C++ is going to run and what commands do.

SQL is entirely dependent on your DB and the data structure. Let it join to your barcode table and don't tell it that products can potentially have more than one barcode and all of a sudden you've got products appearing 5 times because they've accumulated 5 barcodes over the years.

1

u/Mathie1729 Sep 05 '26

The silent row multiplication is the worst. I always check row counts and cardinality on the join keys before/after, because a one-to-many join can silently inflate your sums. It's saved me more times than I'd like to admit.

1

u/elevarq Sep 06 '26

The problem has nothing to do with AI, it’s about an organization that doesn’t test anything. Or doesn’t have structured tests, etc. An organization like this will always have problems like this, no matter who or what wrote the code

1

u/BigMikeInAustin Sep 05 '26

I mean, that part of how I've been checking queries I write for years.

And checking duplicates is becoming extremely required in the warehouse engines that don't even validate keys or constraints. At best, they let you list it in the metadata of the table.

1

u/ChallengeMundane7635 Sep 06 '26

If he knows how to use it but not test it - he does not know how to use it.

1

u/elevarq Sep 06 '26

And without AI the problem might have been even worse…

Your problem is about your internal processes, how things are structured. AI just accelerated the broken process

1

u/reyarama Sep 06 '26

“Plausible looking SQL” are you retarded or something? Not a single experienced dev looks at SQL and thinks “gee well the syntax is correct so it must be good!”. Fucking morons everywhere

1

u/chmod764 Sep 06 '26

My first line of sanity checks are dbt tests on all dbt models. Anyone here who doesn't know what dbt is, I highly recommend reading up on it because it's a game changer.

Also, I know it wasn't OP that was piloting the AI here, but it's absolutely critical to at least give the AI LLM the context of each table's grain (what makes a row unique) if you expect it to come up with anything reasonable. Once again, not saying OP is saying this, but I see a lot of people complain that "AI sucks because xyz" when in reality they gave it nearly zero context. That's a user error. It's still a machine, not a magic mind reader.

I'm willing to bet that most of us do not work on anything so complex that a sonnet 5 model couldn't contribute to as long as it has enough (and the right) context.