5 Ways I Use ChatGPT to Level Up My SQL Skills

MargaretEfron
Learning Data

--

Photo by Bruno Nascimento on Unsplash

As a data analyst, I’ve found ChatGPT to be instrumental in leveling up my SQL skills. I use ChatGPT in many ways, including checking my SQL code for syntax errors, reviewing the error messages, adding comments, and more.

When interacting with ChatGPT about SQL concepts, follow these excellent tips from the

course ChatGPT for Data Analytics:

Be as clear and specific as possible, and include context around table names, descriptions, etc.

Use roles to add context and set the tone and complexity of the response

Provide code samples and ask for summaries or line-by-line explanations

Iterate and use follow-up prompts until you get the focus and level of detail you need (remember — the initial solution may be incorrect!)

Below are five ways I used ChatGPT to improve my SQL skills in just this past week!

1. Ask ChatGPT to check your SQL code for syntax errors.

Before you enter your code in SQL Server, you can enter it in ChatGPT to correct any obvious syntax errors. In this case, I asked ChatGPT: “Can you check this SQL code for syntax errors?” and pasted in my code.

Important disclaimer: If you’re dealing with confidential data, change the name of your table and schema before pasting it into ChatGPT. You can also change the name of certain columns or queries.

I asked ChatGPT to review my SQL code for syntax errors.

Below, you can see that ChatGPT caught the syntax errors, but did not explicitly list out all my errors.

On the last line of code, I deliberately left off the last quotation mark and the semicolon:

WHERE [Last Name] = ‘Miller’ AND [Job Report Date] >= ‘2013–01–01

ChatGPT did NOT list this error in the text box, but it DID correct this error in the SQL code at the bottom.

The lesson here: ChatGPT can help you proofread your SQL queries, but you should read both the list of errors AND the SQL code at the bottom. There may be discrepancies between the two.

2. Learn the differences between SQL functions, and which is the best for your use case.

Sometimes, you may not know the best SQL function to use, and ChatGPT can help you figure this out. Last week, I was working on a data project where I needed to convert data fields to integers at the beginning of the SQL query. I did not know whether I should use the ‘CAST’ or the ‘CONVERT’ function, so I asked ChatGPT:

“What is the difference between the CAST and CONVERT functions in SQL? Which should I use in this case?”

ChatGPT description of CAST function
ChatGPT description of CONVERT function, and when to use CONVERT vs. CAST.

After I described the use case situation to ChatGPT, it advised me:

Given your needs to convert various fields (such as “Signing Bonus”, “Base Salary”, etc.) to integers, and without a specific requirement for formatting (especially for dates and times), CAST would be sufficient and the more standardized choice. Use CAST for broad compatibility unless you have a specific reason to format data during conversion, in which case CONVERT might be necessary if your database system supports it.

3. Describe what you want the SQL code to do in plain English, and ask ChatGPT to help you write it.

Sometimes you know how to say in plain English what you want the SQL code to do with your data, but you don’t know what functions to use and in what order. In that case, ChatGPT can give you a great place to start.

In the example below, I stated the function I wanted to use and which field values I wanted to replace. But even if you don’t know what function to use, you can start by describing what you want to do, and go from there!

I asked ChatGPT to help me write a CASE statement in SQL.

I love how ChatGPT gave me the SQL code and also broke down each of the lines of the statement, so I understand what each line of code is doing and can edit as needed.

4. Edit your SQL code if it isn’t giving you the output you need.

One of my favorite aspects of ChatGPT is the back-and-forth messaging — you can ask it for help, it will give you an answer, and you can push back and ask for a revised response. If the SQL code is not performing the way I want, I can open ChatGPT and give it the following:

  • The code I’m currently using
  • What I expect the output to be
  • What the output actually is
  • Any error message I received

In the example below, I asked ChatGPT to change my SQL code so there is only one column labeled for gender.

I asked ChatGPT to help me modify my CASE statement in SQL.

5. Ask ChatGPT to break down the meaning of SQL code.

If you are reviewing old SQL code and don’t understand what it means, you can copy & paste it into ChatGPT and ask it to break down the query for you. As always, when dealing with sensitive data, remember to change the table & schema name for security purposes before entering into ChatGPT.

In the simple ChatGPT prompt below, I asked it to explain a simple SQL statement:

I asked ChatGPT to help me understand a SQL statement.
ChatGPT broke down each aspect of my SQL code.

Your SQL code may be more (or less) complicated than this. If the SQL code is too long, you can break it up into pieces to digest through ChatGPT, and add comments to help yourself remember what each part means.

For example, in the ChatGPT prompt below, I provided my SQL code and asked ChatGPT to add comments so I could remember what each part does.

I asked ChatGPT to help comment out my SQL code.
ChatGPT added comments to my SQL code.

ChatGPT added comments to my SQL code — but too many! I did not need [First Name] to be commented that it refers to the first name, and [Last Name] to be commented that it refers to the last name, and so on.

However, there were some helpful comments towards the end:

WHERE [Last Name] = ‘Miller’ — Filter results to only include students with the last name ‘Miller’

AND [Job Report Date] >= ‘2013–01–01’; — Further filter to include only job reports from 2013 onwards

I asked ChatGPT to revise the SQL code above to only include comments for the lines in the WHERE clause since I found those the most helpful.

Final Thoughts

ChatGPT is excellent for improving your SQL skills, including:

  • translating your prompt from plain English into SQL code (or at least giving you a good starting point!)
  • commenting out your code so you understand every line of your query
  • troubleshooting error messages or editing code to lead to the output you want
  • describing the difference between functions and helping you identify the best one for your use case
  • giving you the basic syntax for a SQL statement(e.g. “What is the basic syntax to create a table in SQL?”)

Whether you’re starting as a data analyst, or have been in the analytics field for years, ChatGPT is an essential tool to have in your toolbox.

Further info:

For more information on ChatGPT and data analytics, check out the free

course “ChatGPT for Data Analytics”, which uses ChatGPT and Bard to work smarter & faster with Excel, Google Sheets, Power BI, SQL, and Python.

The contents of external submissions are not necessarily reflective of the opinions or work of Maven Analytics or any of its team members.

We believe in fostering lifelong learning and our intent is to provide a platform for the data community to share their work and seek feedback from the Maven Analytics data fam.

Happy learning!

-Team Maven

--

--

MargaretEfron
Learning Data

I love all things data and write about Excel, Power BI, and SQL. I currently work as a Business Systems Analyst at the Darden School of Business.