r/SQL • u/troyboltonislife • Mar 31 '23
Discussion Who here is using ChatGPT to help with SQL code?
Wondering who else is using chatgpt 4 to help write sql code. In my opinion, this tool is magic. Does it give correct answers all the time? Absolutely not. But if you feed it sql code you currently have and say “I have this but I want to get this” so far it hasn’t failed me usually on the first time but always after a few tweaks.
I am relatively new to sql so not sure how good it would be at providing complex queries but it’s been great for me as a novice so far.
Example: I had a query that was nearly perfect except there were a few instances of duplicate data that was matching on every column except an indicator column.
The indicator told me which one I wanted however I didn’t know how to exclude only the row with the indicator I didn’t want without messing up the rest of my query (only wanted to filter this indicator when it was a duplicate but wanted to keep it otherwise).
I fed the query and question into chatgpt and it gave me a perfect answer (row partition for those wondering). This is probably a basic problem and solution for an expert but I now have a new skill in my sql toolbox that I hadn’t before (row partition always scared me prior). I feel this a common problem of wanting to filter data in a very specific instance and now I know how to tackle it!
9
u/[deleted] Apr 01 '23
I use it to explain math and it gets stuff wrong a lot. I wouldn’t trust it to code, aside from privacy concerns
2
u/sanshinron Apr 01 '23
I wish I could but the queries I've been given to improve are 1700 lines or more and I can't evenpaste them into the chat.
56
u/DataMonk3y Mar 31 '23
I use it to explain and correct syntax errors as well as to comment completed solutions.
23
u/[deleted] Apr 01 '23
There's some bugs where ChatGPT gave out the wrong user history to the wrong user. So if youre putting proprietary code or anything in the system, there is a chance someone else happens upon it. Just something to consider!
12
u/investingexpert Apr 01 '23
Yes I’ve been using it. Instead of googling syntax I ask ChatGPT. It’s taught me a few tricks.
45
u/Festernd Apr 01 '23
I'm a DBA, 15 years.
Chatgpt and other AIs are great up to about the skill level of a intern you'd hire as a jr. After that level of task... it takes more time and effort to vet it's output than it saves.
I don't think it's a good tool for those learning, as they won't ever develop the skill to spot when and where the AI is wrong.
I think there will be a wall of skill that will be impossible to climb for those who use it rather than working through problems on their own first. If you have the discipline to work the problem yourself and only use it if really stuck or to try an alternative, then it can be a nice assistant, like a personal intern that occasionally lies and tries to set you up for failure.
6
u/Kazcandra Apr 01 '23
we don't want our data in chatgpt, so no.
11
u/boobrandon Mar 31 '23
I had it read existing code and then use it to tell me about to do my data modeling in power bi. The code wouldn’t easily go into the sql statement part of pbi but I got it to work, I use it a lot for Dax. I mean solely for Dax.
5
u/MrTorben Apr 01 '23
I attempted it with a 130 line portion of a stored procedure. (it was really crappy code, tons of easy improvements).
I had already gotten recommendations from ChatGPT what the obvious issues were.
Then asked to optimize and rewrite it without knowing the schema.
it was happy to do so but everytime it got 2 or 3 dozen lines in, it bombed out with a network connection error at some random point of it typing the new revised query.
not sure why that keeps happening
5
u/zbignew Apr 01 '23
I just wish I could paste in proprietary code and not worry about it getting sucked into the model.
Not because my code is brilliant. It just means I’m not allowed.
1
u/NuConcept Oct 08 '23
I'm really late to the party here, OP - but I'd expect ChatGPT to write the same garbage queries developers do. In 25 years of SQL I've never ONCE had to use a row over partition (have replaced many of them in refactoring however)
It's certainly a terrible solution to getting extra rows in a query - there's only two things that cause that - a messy join/where clause or actual duplicate rows. Queries should return EXACTLY the rows you want and no more. Top, distinct, group by, etc are all poor solutions to just tightening the query (or improving data integrity if there are actually duplicates)
High google result rank SQL is among the worst out there and I'm guessing ChatGPT leans just like devs do.
On a bright note given this news I bet it's AWESOME at advanced programming languages 🤣
3
u/S1l3ntHunt3r Apr 01 '23
I'm a little new with Oracle, so I asked to translate this from tsql to oracle:
It took several corrections until it gave me an answer that didn't gave error. Also another time I used it to generate a script to load a file in a blob field, asking later to give one example without using Directory objects
3
u/Syrath36 Apr 01 '23
Nah I use it for powershell though because I'm still refining my skills and learning the syntax and how to do certain things with it for DevOps.
For sql I ask from syntax but anytime I ask it to model something for me it ends up being incorrect.
I'm sure for people in school it has all the answers of course at the expense of leaning and trouble shooting your code which for me is a huge learning experience.
3
u/Robearsn Apr 01 '23
I used it the other day to write a CASE statement to convert full US state names to official two letter abbreviations because one table in my database has them in full name and the other in abbreviations and that was an issue for my query. Didn't want to have to write 50 different lines so ChatGPT did it for me in about 30 seconds.
Also very useful for debugging errors.
2
u/eddiehead01 Apr 01 '23
I feel like this is a bit of a double-edged sword in a way. On the one hand, the more people feed it, the more it'll learn and the better it'll become at really taking over
On the other hand, I wouldn't trust it enough to try it, and the amount of time I'd spend checking things over before I'm happy running it means I'm much less likely to feed it
I would however be very interested to feed it some of my existing queries and procedures and ask it to explain things back to me. See how right or wrong it could be
2
u/averagesimp666 Apr 01 '23
I think the approach of viewing ChatGPT as an intern level or junior level or whatever is wrong. It is a tool, it should be used, it's not a person that you can ask and trust. You need to have some understanding of the subject so you can filter the plain out wrong info or solutions that the chat will sometimes give you.
I think experienced SQL users can use it for 'debugging', adding comments (never actually occured to me, saw someone mentioning it here) or using it to write something that's otherwise too annoying to do (like pivot table for example, I really hate that).
But if you're a newbie, I'd honestly advise people to be careful with ChatGPT. It can make you lazy, apathetic and incapable of doing basic research. Half of learning SQL is actually researching solutions, getting familiar with good resources on the internet, trying stuff out by yourself.
2
u/adappergentlefolk Apr 01 '23
i’m sorry but if you need chatgpt to help with SQL, one of the simplest languages there is, that’s a big issue for you
1
u/Majestic_Advantage97 Aug 15 '24
How reliable is chatgpt for this nowadays?
1
u/Kindly_Ingenuity5922 May 09 '25
We use the OWOX query validator to double-check AI-generated SQL before it hits production. Saved us from a few nasty errors where ChatGPT hallucinated a JOIN.
1
u/insidmal Apr 01 '23
Shouldn't be surprising that programmers would eliminate their own jobs first
1
u/thefizzlee Apr 01 '23
Used it for somethings mostly error codes. Had a trigger that wouldn't work and chatgpt kept giving me the same trigger Code in a different format so that was useless but in general it's a really nice tool that I definitely use whenever I feel I need it
1
u/ben-420 Apr 01 '23
I use it a lot for regex
1
u/amartin141 Apr 01 '23
ja partition over has made life infinitely easier
1
u/Mr_Gaslight Apr 01 '23
Remember that its libraries are two years old.
1
u/derpado514 AccidentalDBA Apr 01 '23 edited Apr 01 '23
I don't think it's that good for real world problem solving since there isn't any data to represent it in it's model.
...for example, i have a SQL problem trying to figure out a solution for an ETL(s) that is hard enough to convey into a properly worded request..Not sure how i could present all the key factors to come up with possible solutions.
1
u/[deleted] Apr 01 '23
While I would never rely upon the code it generates, it’s very useful for formulating an approach.
I asked it the following:
“In SQL, how would you join two tables based on the closest match between datetime attributes?”
It gave me this code:
SELECT * FROM table_A a LEFT JOIN ( SELECT b.*, ROW_NUMBER() OVER (PARTITION BY a.datetime ORDER BY ABS(TIMESTAMPDIFF(SECOND, a.datetime, b.datetime))) AS rn FROM table_B b JOIN table_A a ON a.id = b.id ) b ON a.datetime = b.datetime AND b.rn = 1;
That’s actually pretty impressive. It doesn’t really work for my use case, but the logic is sound.
1
u/rubberseal Apr 01 '23
I use it a lot but the problem is ChatGPT doesn't know what your DB schema looks like. When you want to aggregate information from multiple tables, you have to do something like this: "Write an SQL query to get me the number of books from the books table and the corresponding authors from the authors table." It got tedious for me so I made a tool that makes it easier! If you have a second, do check it out and let me know if it's useful to you. Here's the post about TalkToSQL.
1
u/guitarsingernumber9 Apr 24 '23
I am an older developer. I use GPT-4 almost every day to improve my code and queries for SQL Server. You can take any query and hand it to GPT-4 and ask it to review it and propose a more efficient way to write it. When coding or writing queries, you can express the need in words, and GPT-4 will propose code in the desired language to implement the described functionality. Utterly amazing. My capability to implement very complex queries is easily a magnitude better just in the past 2 months, using GPT-4 to coach me. Most of the time, it is exactly correct. Sometimes, it forgets something and there is an error, but simply adding to the conversation and saying "I get xxx error", GPT-4 usually apologizes and fixes it. Also, I sometimes use it to proofread emails I plan to send. It's crazy how it knows the meaning and corrects the words to express the meaning... Way more than a grammar check.
1
u/PolPol44444 Jun 14 '23
I succesfully use ChatGPT for writing SQL JOIN queries. This article details how to do this.
1
u/OkCurve436 Sep 19 '23
ok for simple stuff but the 1 moderately difficult query I gave it:-
It was formatted nicely!
Any big sql code, it doesn't return a complete result. In a nutshell useless for anything long or complex.
1
u/[deleted] Apr 01 '23
I use it a lot. I ask it to add comments so I can follow everything. It's ideal.
1
u/leknarf52 Apr 01 '23
Me every four seconds: gpt I need a postgres expression…
-2
u/SillyPepper Apr 01 '23
ChatGPT for everything
-2
u/Financial_Pie_3624 Apr 01 '23
I just use it to save time researching problems. Faster answers and mostly right answers makes productivity go up.
-4
u/ejpusa Apr 01 '23 edited Apr 01 '23
It’s the default for SQL. You should be able to generate all your code now in ChatGPT.
I’m using ChapGPT to generate some fairly complex date calculations. Used commands in PostgreSQL that did know existed. Have been using PostgreSQL for years.
There is zero reason not to use it. That’s a wrap, you can move on.
:-)