How I Successfully Used ChatGPT to Write SQL Queries at Work?

Today, I will share how I’ve been getting support from ChatGPT for writing SQL queries in my professional career. I will explain how I tackled a complex SQL query challenge and how I managed to have ChatGPT help me prepare it within seconds at work.

As a professional software developer, we frequently use SQL queries in our work. Sometimes, we might need to write slightly complex SQL queries to achieve the desired results, pulling data from tables. Creating these intricate queries can require considerable thought and effort. While this process contributes to our personal growth, I’ve recently significantly shortened the time spent on it by seeking assistance from ChatGPT for writing these complex queries.

Things to Know Before Getting Started

  • ChatGPT has a wide range of knowledge when it comes to databases. It can assist you with many popular database applications, from MySQL to MongoDB, Oracle to DB2, and more. It’s capable of helping you and generating queries for various database systems.
  • Just like with humans, in complex problems, the initial response from ChatGPT might not always be the most accurate. As you provide new information, give feedback, and engage in further thought, ChatGPT’s response continues to evolve and improve. Therefore, you don’t have to solely rely on the first answer given by ChatGPT. There might be gaps or errors in that response. By continuing the conversation and iterating through various interactions, you can achieve results that are closer to your desired outcome and more accurate over time.
  • When it comes to professional matters, I advise against providing ChatGPT with table names and column names specific to your company. This is because the information exchanged here, along with the conversation history, could potentially be used as data for training ChatGPT. Instead, I recommend conveying table names and column names by altering them before sharing them with ChatGPT.

Can ChatGPT Generate SQL Queries?

Absolutely, ChatGPT can generate both simple and complex SQL queries to match your specified conditions. Moreover, it can craft these queries in the appropriate syntax for various databases such as DB2, Oracle, MySQL, or MongoDB. With its advanced natural language processing capabilities and extensive training on a vast dataset, ChatGPT can comprehend your requests and translate them into SQL queries. Therefore, you can indeed seek assistance from ChatGPT for writing SQL queries.

How I Used ChatGPT to Generate SQL Queries at Work?

At the software company where I work, we need to prepare an SQL query to retrieve records with specific attributes from our table for the project we’re working on. I realized that this task can’t be accomplished with a simple SQL statement, so I started thinking about it. Later on, it occurred to me that I could use ChatGPT to assist with this.

Problem: We needed to find a list of all customers who had records in the table with the same error code for the past 30 days. These customers were required to have records in the table with a specific error code consistently every day for the last 30 days, with at least one occurrence per day.

1. Explaining the problem to ChatGPT

ChatGPT, while being an advanced language model, it’s not aware of information you haven’t provided or discussed. Therefore, a crucial aspect of obtaining effective support from ChatGPT is accurately conveying the problem.

To do so, I began introducing the problem to ChatGPT. I informed ChatGPT about our table, columns, and the specific attributes of customers I wanted to retrieve. However, as previously stated, I refrained from using the actual table and column names when providing them to ChatGPT. This is due to the fact that the conversation history might be used by OpenAI as part of their training dataset. With data security in mind, I definitely didn’t share my company’s actual table and column names. Instead, I used mockup names in their place.

Pro Tip: Do not share with ChatGPT the actual names of columns and tables of your database.
Learn more: Is ChatGPT safe to use when coding? 

2. Sending the Prompt and Getting a Response

I sent a prompt to ChatGPT as shown below. And ChatGPT quickly prepared the SQL SELECT statement for me:

chatgpt generates sql query

3. Reviewing and Requesting New Iterations.

After conveying our request to ChatGPT, it promptly delivers us a response. Our next step is to examine the response provided by ChatGPT and determine its accuracy. It’s important to note that there is no rule stating that the response from ChatGPT will always be correct.

If you believe there is an error or omission, it’s not a problem. This doesn’t mean that ChatGPT has failed to fulfill your request. As mentioned earlier, you can ask for new iterations from ChatGPT and request it to correct its mistakes by pointing them out. This way, the quality of your response will improve with the new answers provided by ChatGPT.

Pro Tip:Expressing your problem to ChatGPT by typing it at the keyboard can be both time-consuming and exhausting. Instead, I recommend using the default feature that comes with the ChatGPT mobile app called WhisperAI. WhisperAI can convert your speech into text. This allows you to communicate with ChatGPT simply by speaking, saving you time and providing you with the opportunity to express the problem to it in more detail.

Learn more: How to use WhisperAI in ChatGPT?

Therefore, after sending my question to ChatGPT, I reviewed the response it provided me. I couldn’t find any mistakes in it. However, if I had noticed an error, I would have asked it to make revisions and provide a new response while informing it about the mistake.

In fact, I was so impressed with the response from ChatGPT that it provided me with an answer I could think about for 5-10 minutes in a matter of seconds. All I need to do now is integrate the table and column names from the SQL Query provided by ChatGPT with the actual names in my program, and then test it.

Should You Seek Assistance From ChatGPT While Working?

Yes, you can seek assistance from artificial intelligence models like ChatGPT even while working. Integrating them into your professional routine can be incredibly beneficial. Thanks to the example provided, I was able to save a significant amount of time. I could then invest this time into enhancing and improving my program across various areas.

  1. ChatGPT is available 24/7 and is free to access.
  2. It is knowledgeable not only in a single database but also in various database syntaxes, making it a versatile asset for your needs.
  3. You can engage in interactive dialogues with ChatGPT, and it can promptly correct its mistakes when you point them out. This enables it to support you in software matters like an additional team member.

For these reasons, you can and should benefit from ChatGPT in your professional work life. However, it’s important to exercise caution and avoid sharing sensitive or confidential information directly with ChatGPT. You can either use mock-up methods for such data or seek assistance without disclosing those specific details.