How to Use ChatGPT to Write SQL Queries
A practical guide to getting correct, readable SQL out of ChatGPT, from simple selects to joins and optimisation, with the context it needs to get it right.
Eddie Ochieng
July 6, 2026

SQL is one of the things ChatGPT is genuinely great at, because queries follow clear rules and the model has seen millions of them. Whether you are a developer who writes SQL occasionally or an analyst who would rather not memorise join syntax, it can save real time. The catch is the same as always, it needs to know your tables. Give it that, and the queries it writes are usually correct and often cleaner than what you would write by hand.
Give it your schema first
The single most useful thing you can do is describe your schema. Tell ChatGPT your table names, the important columns, and how tables relate. Without that it guesses at column names, and a query against columns that do not exist is worse than none.
Writing a query from a description
Start by giving it the tables, then ask in plain language for what you want. The more precise the request, the better the query.
I have a table called orders with columns id, customer_id, total, and created_at, and a table called customers with id and name. Write a SQL query that returns the top 5 customers by total amount spent, with their names.
Because the prompt names the tables, columns, and exactly what to return, ChatGPT can write a correct join and aggregation the first time.
Understanding a query someone else wrote
Inherited a gnarly query you do not understand? Paste it in and ask for a plain explanation. This is one of the fastest ways to learn SQL, because you see real queries explained in context.
Explain what this SQL query does step by step, including what the join and the group by are doing, as if I know the basics but not advanced SQL.
Fixing and optimising
When a query is slow or wrong, describe the problem. ChatGPT is good at spotting a missing index suggestion, a needless subquery, or a join that should be a different type.
This query works but runs slowly on a large table. Here it is. Suggest how to make it faster and explain why each change helps.
Ask it to explain the why, so you learn the principle rather than just pasting a faster query you do not understand.
Switching between dialects
SQL varies between databases, and this is where ChatGPT quietly saves you. Ask it to write for your specific system, or to convert a query from one to another.
Rewrite this MySQL query so it works in PostgreSQL, and point out any differences I should know about.
A hard rule with SQL
Never run an AI written query that modifies data against a production database without testing it first. An UPDATE or DELETE with the wrong condition is unforgiving. Test on a copy, check the row count, then run it for real.
FAQ
Do I need to know SQL to use this?+
It helps but is not essential for simple queries. For anything that changes data, you should understand what the query does before you run it, which ChatGPT can also teach you.
Why does it use column names that do not exist?+
Because it is guessing without your schema. Always give it your real table and column names and the problem disappears.
Can it handle complex joins and window functions?+
Yes, and well, provided you describe the tables and what you want clearly. Complex SQL is where the time savings are largest.
Is it safe to paste my schema in?+
Share table and column names, not real customer data. Never paste sensitive records into a chatbot. The structure is enough for it to write the query.
For turning query results into insight, see how to use ChatGPT for data analysis. For broader coding help, here is our guide to ChatGPT for programming help.





