How to generate SQL statements with ChatGPT
1–10 of 48 posts
Re: How to generate SQL statements with ChatGPT
#2by the time i figure out what to write in the prompt most of my work is done.
Re: How to generate SQL statements with ChatGPT
#3The end result is that you can produce safe SQL queries on behalf of your users, allowing your users to use natural language to query their data in your database.
For example, in this demo[2], I ask it:
query("how much have i spent renting movies, broken down by month")
And it responds correctly, guaranteed to be constrained to my data.1. https://github.com/amoffat/HeimdaLLM 2. https://github.com/amoffat/HeimdaLLM/blob/dev/notebooks/demo...
Re: How to generate SQL statements with ChatGPT
#4Re: How to generate SQL statements with ChatGPT
#5Thought I'd give it a try and gave chatGPT 2 examples of existing tables and the corresponding TS types and asked it if it understood what it should do next. It explained the existing logic and also 2 edge cases hidden (e.g. if a type was boolean the column was UInt8 (to save 0/1))
Then I pasted all types in batches of 15 in and it generated with only 2-3 corrections needed, probably saving me around 1-2h of manual checking and/or creating. It understood that certain fields are non LowCardinality and therefore not used it.
Re: How to generate SQL statements with ChatGPT
#6If you do this, it will automatically retry with a new SQL query if it gets any error messages.
Re: How to generate SQL statements with ChatGPT
#7I had to forbid my coworkers from using chatgpt to generate SQL because it kept doing things slightly wrong - like missing parens when dealing with AND and OR in a where statement.
Re: How to generate SQL statements with ChatGPT
#8the difficulty at my job is the many tables have no documentation, the transform code is inaccessible, and they were created by different teams by people who arent here any more. by the time i figure out what to write in the prompt most of my work is done.
At some point some startup is going to come up with a backend-in-a-box where you just describe the tables, rules and it will spit out all Graphql mutations and queries, react components, an auth layer, etc...
ChatGPT out of the box can do all of that right now, but it needs a coordinating tool, perhaps to even make unit tests and alter statements (for changes) and mostly, so that we don't blow out the context. That's the key issue - context. If context was unlimited, then we can just emit everything in one go. So initial structure and some prompting should lead to everything else.
Re: How to generate SQL statements with ChatGPT
#9Fortunately, the queries themselves are small, so the rest of tokens are spent on context that include index names and schema.
This doesn't necessarily scale well in other contexts and can get pricy quick!
Re: How to generate SQL statements with ChatGPT
#10If you know SQL why is this better than just writing SQL? If you don’t know SQL how do you know it’s correct? I had to forbid my coworkers from using chatgpt to generate SQL because it kept doing things slightly wrong - like missing parens when dealing with AND and OR in a where statement.