Finding Uses for ChatGPT: Building SQL Queries.
I will admit that I was not very fond of ChatGPT when I first learned of it. I thought of it as just an overhyped gimmick that basically googled things for you, and let’s be honest, that’s kind of is what it is. But I must confess, I have been finding some uses for it. Nothing as spectacular as some claim online, such as, it made them rich, or it taught them how hack into anything.
I am finding ways to utilize ChatGPT to find information faster than I could before on google, and to help create code examples tailored to what I’m working on. That brings me to the point of this article.
Using ChatGPT to Create SQL Queries.
I will be showing you how you can use ChatGPT to help write SQL queries that you might be struggling with. There are two methods I will cover, one will be using the standard ChatGPT website, and the other will be integrating ChatGPT into a database program called DBeaver.
Disclaimer: I am not an expert in working in databases, don’t execute anything in your production database you are unsure of.
Using the Web Interface.
This is fairly straight forward, we are going to use the normal web interface (https://chat.openai.com/) to ask ChatGPT to write an SQL query for us. Let’s start with something simple: “write an sql query to select all items from table”

That’s pretty basic, we could have googled how to do that quickly, we don't need ChatGPT for those kinds of queries. Where it becomes useful is when you ask it to create queries using detailed information, such as table and column names from your database.
For Example: “write an sql query to count how many people live in new york city from the table population”

You can see that this gives us a more customized answer that can be tailored to your database. We can get even more detailed and have ChatGPT return queries that work perfectly with our database.
For Example: “help me write an SQL statement. I want to select Zone1Id from the table zone_table and match Zone1Id to the Id from table area_table and show them by Name”

This example had ChatGPT create an SQL Query that compares the “Zone1Id” from the table “zone_table” to the “Id” of the table “area_table” if they matched it lists the results by the “Name” column from the table “area_table”
The output that ChatGPT gave me was good enough to copy and paste into a query on my database and give me the results I was looking for. You may not always get the exact query you are looking for on the first try, you may need to reword how you ask ChatGPT or it may give you a result close enough to help you complete it correctly the way you were needing.
It is also useful to read the explanation ChatGPT gives after each answer, it explains what the query does and how it works.
Integrating into DBeaver
You can also integrate ChatGPT into a database tool called DBeaver. I won't cover the step by step instructions of installing DBeaver and setting it up in this article, but here's an overview of getting ChatGPT integrated once you have the program installed:
- You can install the free Community Edition of DBeaver at https://dbeaver.io/
- You will Need an API key from OpenAI for the integration. This is a seperate account from the main ChatGPT web interface. https://platform.openai.com/
- Follow these instructions on getting it setup in DBeaver. https://dbeaver.com/docs/wiki/AI-Smart-Assistance/#install-gpt-extension
Once you have the extension installed the queries will load right into DBeaver from ChatGPT to run against your database. There is a cost associated with using the api, but for personal use it seems to be fairly inexpensive, and as of this writing you get a $5 credit on the account when you sign up.
Conclusion
While I will still continue to be a hater on all of the AI hype, I do see uses for it. I think there are good ways it can be used as a tool to help people learn an accomplish tasks more efficiently. Hopefully this article helped someone accomplish their tasks a little easier as well.