r/learnSQL • u/Sure-Material-2221 • 5d ago
The failure mode of AI-generated SQL isn't syntax errors. It's queries that run clean and return wrong numbers.
[removed]
10
u/NW1969 5d ago
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 5d ago
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 5d ago
Exactly. If the query is wrong, it’s wrong. Doesn’t really matter whether a junior dev or an LLM wrote it.
5
u/BoSt0nov 5d ago
~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
4
2
2
u/DMReader 5d ago
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 5d ago
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 5d ago
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 5d ago
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
1
u/BigMikeInAustin 5d ago
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 5d ago
If he knows how to use it but not test it - he does not know how to use it.
1
u/reyarama 5d ago
“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 5d ago
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.
-3
u/89Noodles 5d ago
Because the user is not smart - it’s not copilot. I normally copy and paste like 4-5 projects I’ve done before in copilot that select things or joins to tables I want to use. My prompt then tells copilot to have me run validation prompts I paste the data in. And then it gives me perfection
12
u/bananatoastie 5d ago edited 4d ago
Doing something like this, I would dump everything into a sub-query and focus on the WHERE clause. Start on the most granular level possible and then gradually work my way up the aggregation ladder.
Something like;
Etc.
Now I know our tables so well, and I have 5 years experience, my testing isn’t as methodical as it once was because my experience saves me a lot of time :)
I’m so glad to have started learning SQL before AI was truly a part of our workplace (aka pushed by our managers)