Live data from Hacker News

Getting AI to write good SQL

cloud.google.com

81–90 of 379 posts

Re: Getting AI to write good SQL

#81

In real life I find using AI for SQL dangerous. It allows people that don't know what they do to write queries that can significantly impact servers. In my world databases are relatively big for most developers, but not huge. Sometimes when I want to fine tune a query I am challenging AI to provide a better solution. I give it the already optimized query and I ask for better. I never got a better answer, sometimes be…

The strategy I've used with these people is to let them prototype with AI and then have them hand over their work to me where I can then make it significantly more efficient. The nice thing is that their poor performing version acts as a reference for validating the output of my queries.

Re: Getting AI to write good SQL

#82
post #45

For the problems where it would matter the most, these tools seem to help the least. The hardest problem domains don't have just one schema to worry about. They have hundreds. If you need to spin up a personal blog or todo list tracker, I have no doubt that Google, et. al. can take you exactly where you want to go.

and then add in ambiguity in the business terms / intention behind the query. still a big need for something like semantic layer or ontology to sit between business and at least right now that stuff hasn’t been automated away yet (it should be though)

Malloy [1] has a semantic layer [2]... and Model Context Protocol (MCP) support is being added through Publisher [3]. Something to keep an eye on. Seems like a great fit for LLMs.

[1] https://www.malloydata.dev/ [2] https://docs.malloydata.dev/documentation/user_guides/malloy... [3] https://github.com/malloydata/publisher

Re: Getting AI to write good SQL

#83
post #35

> We will cover state-of-the-art [...] how we approach techniques that allows the system to offer virtually certified correct answers. I don't need AI to generate perfect SQL, because I am never going to trust the output enough to copy/paste it — the risk of subtle semantic errors is too high, even if the code validates. Instead, I find it helpful for AI to suggest approaches — after which I will manually craft the S…

Hopefully your trust in yourself is warranted

Re: Getting AI to write good SQL

#84

Can I just say that Google AI Studio with latest Gemini is stunningly, amazingly, game changingly impressive. It leaves Claude and ChatGPT's coding looking like they are from a different century. It's hard to believe these changes are coming in factors of weeks and months. Last month i could not believe how good Claude is. Today I'm not sure how I could continue programming without Google Gemini in my toolkit. Gemini…

It always is for the first week. Then you find out that the last 10% matter a lot more than than the other 90%. And finally they turn off the high compute version and you're left with a brain dead model that loses to a 32b local model half the time.

Re: Getting AI to write good SQL

#85

All this LLM written SQL stuff sounds great until you realize if you don’t really know SQL you won’t be able to debug or fix any broken SQL an LLM generates. Thus, this is mainly just a tool for true experts to do less work and still get paid the same, not a tool for beginners to rise to the level of experts.

Have you not actually used LLMs? Just copy in the errors and away it goes.

Re: Getting AI to write good SQL

#86
post #35

> We will cover state-of-the-art [...] how we approach techniques that allows the system to offer virtually certified correct answers. I don't need AI to generate perfect SQL, because I am never going to trust the output enough to copy/paste it — the risk of subtle semantic errors is too high, even if the code validates. Instead, I find it helpful for AI to suggest approaches — after which I will manually craft the S…

Explain that to the average manager or junior engineer, both who don’t care about your desire to build well but not fast.

> So now that we brought down prod for a day the new rule is no AI sql without three humans signing off on any queries.

Re: Getting AI to write good SQL

#87
post #86

Earlier quoted context omitted.

Explain that to the average manager or junior engineer, both who don’t care about your desire to build well but not fast.

> So now that we brought down prod for a day the new rule is no AI sql without three humans signing off on any queries.

If that’s the scenario, I would be asking why the testing pipeline didn’t catch this rather than why was the AI SQL wrong.

Re: Getting AI to write good SQL

#88
> Even with a high-quality model, there is still some level of non-determinism or unpredictability involved in LLM-driven SQL generation. To address this we have found that non-AI approaches like query parsing or doing a dry run of the generated SQL complements model-based workflows well. We can get a clear, deterministic signal if the LLM has missed something crucial, which we then pass back to the model for a second pass. When provided an example of a mistake and some guidance, models can typically address what they got wrong.

Sounds like a bunch of bespoke not-AI work is being done to make up for LLM limitations that point blank can’t be resolved.

Re: Getting AI to write good SQL

#89

All this LLM written SQL stuff sounds great until you realize if you don’t really know SQL you won’t be able to debug or fix any broken SQL an LLM generates. Thus, this is mainly just a tool for true experts to do less work and still get paid the same, not a tool for beginners to rise to the level of experts.

Have you not actually used LLMs? Just copy in the errors and away it goes.

Error goes away but it gives the wrong result.
Post reply on HN