r/learnSQL • u/Sure-Material-2221 • 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
r/learnSQL • u/Sure-Material-2221 • Sep 05 '26
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
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.
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/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.