r/SQLServer • u/qx2 • Jan 11 '26
Discussion AI SQL Query experiment
I gave google AI a shot at creating a sales query joining Sales Orders, Order Details, Order Payments, Order adjustments, Order Locations, Payment types, orders types, payment types, item cost and depletions, it took me 6 hours to get right results. Btw, I had to define all tables, and columns. Am I slow or is the model slow because I had to dictate show it what to group by, filter on sort by to create fact table?
0
Upvotes
3
u/willyam3b Jan 12 '26
I'm having a bit of a hard time understanding how you might benefit from this situation? I mean, having to do this much work just to see if it can define a query correctly? It's interesting, sure, but I'm honestly a little perplexed. My fear is, how will analysts or sales staff or whoever know that their queries are giving them accurate data? (Yes, yes, I'm old, get off my lawn, etc.). I see the advantages with entry-level development, but I'm just not getting it yet for the data world I guess.
2
u/mergisi Jan 16 '26
The 6 hours of schema definition is the real bottleneck with most AI tools. They don't understand your data model unless you spell it out.
A few tips:
For sales queries, the ORDER -> ORDER_DETAILS -> PAYMENTS chain usually trips up AI because of the 1-to-many relationships.
I built AI2sql.io to handle this better - you define your schema once and it remembers the context. Might save you time on the next experiment.
6
u/RobCarrol75 Jan 11 '26
If you use GitHub Copilot and use SSMS 22 (or VS Code with the SQL Server extension), it will use the current database connection as context and you don't need to define all the table schemas, etc. in your prompts. Copilot will just pick all this up for you.
1
u/qx2 Jan 11 '26 edited Jan 11 '26
Love your suggestions, thank you. will give them a try
1
u/qx2 Jan 17 '26
Not related to my post, but why is everything posted on this sub downvoted to NULL?
4
u/kagato87 Jan 11 '26
Maybe a bit of both? It makes a lot of assumptions, and if it doesn't have a schema reference it'll just guess. (Even with the schema sometimes it still does it...) It's also prone to pretty much every badly performing and poor readability patter on the planet.
It usually gets the joins right. It'll use a subquery when a CTE would be identical but readable and strip out all your comments though...
3
u/kassett43 Jan 11 '26
I have found that the choice of Ai model can make a huge difference. With SQL, I've found that the smaller and simpler models, like gpt-4o, are better than the newer 5x models for this use case.
3
u/Comfortable-Zone-218 Jan 11 '26
Just an FYI, but Claude is better at SQL than chatGPT.
2
u/AccessHelper Jan 11 '26
I've had success doing that sort of thing with Chatgpt. What are some of the prompts you were using?
2
u/aSystemOverload Jan 11 '26
Write a script to export your schemas to csv... Give it to your AI... Then use it to help write scripts
2
u/Codeman119 Jan 12 '26
Yeah, when I use AI to help write queries, I don’t use any more than two tables. Anything more than that and it’ll start hallucinating and get lost.
2
u/Appropriate-Rush915 May 11 '26
Take a look at SsmsAgentic.io — a Claude extension I've built for SSMS 22 that reads your live schema directly, so you don't have to define tables and columns manually. It just knows what's there. Queries, views, and stored procedures, all from inside SSMS, reusing your existing connections.
Your 6-hour session would've been a lot shorter. You'll thank me later.