How to Use ChatGPT to Write SQL Queries
A hands-on walkthrough with the real SQL ChatGPT wrote, from a query off a schema to spotting an ambiguous ask and translating MySQL to PostgreSQL, judged by a developer.
Eddie Ochieng
July 6, 2026· Updated Aug 26, 2026

SQL is one of the things ChatGPT is genuinely good at. Queries follow clear rules, there is a mountain of public SQL to learn from, and the answer is easy to check by running it. So rather than describe how it does, I gave it real schemas, asked the kind of things I actually ask, and judged the SQL as a developer. This was ChatGPT on the free tier, August 2026.
The one rule
The single most useful habit is to give it the tables first. Every good answer below started by handing ChatGPT the schema, the column names, and exactly what I wanted back. Skip that and you get plausible SQL for tables you do not have.
Writing a Query from a Description
Give it the tables, then ask in plain language. Here is the prompt, and the query it wrote.
I have two tables: orders(id, customer_id, total, created_at) and
customers(id, name, country). Write a SQL query that returns the top 5
customers by total spend in 2025, showing name, country and total spent,
ordered by total spent descending.SELECT
c.name,
c.country,
SUM(o.total) AS total_spent
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2025-01-01'
AND o.created_at < '2026-01-01'
GROUP BY c.id, c.name, c.country
ORDER BY total_spent DESC
LIMIT 5;It was correct and readable, and it did one careful thing I had not asked for. Instead of filtering the year with something brittle, it used a half-open date range, from the first of January up to but not including the next January.
WHERE o.created_at >= '2025-01-01' AND o.created_at < '2026-01-01'That quietly avoids the classic bug where an order timestamped late on the 31st of December falls outside a naive range. It is the instinct of someone who has been bitten before, and it is the kind of thing that makes ChatGPT a useful pair for SQL rather than just an autocomplete.
When Your Request Is Vague
Then I under-specified on purpose, to see what it would do with an ambiguous ask.
Using those same two tables, give me my best customers.It did not stop to ask me what "best" meant. It picked a definition and, to its credit, said so out loud before writing a line of SQL.
Assuming “best customers” means the customers who have spent the most overall:
That is the pattern to watch. ChatGPT will rarely pause to ask a clarifying question, it will assume and move on. Here the assumption was reasonable, but if "best" had meant most orders, most recent, or highest margin, I would have got a confident, correct-looking answer to the wrong question. Either spell out what you mean, or read its stated assumption before you trust the result.
Switching Between Databases
SQL varies between databases, and this is where it quietly saves time. I asked it to move a MySQL query to PostgreSQL and flag the differences.
Rewrite this so it works in PostgreSQL instead of MySQL, and point out any
differences I should know about:
SELECT id, DATE_FORMAT(created_at, '%Y-%m') AS month
FROM orders WHERE total > 100 LIMIT 10;SELECT id, TO_CHAR(created_at, 'YYYY-MM') AS month
FROM orders
WHERE total > 100
LIMIT 10;It correctly swapped MySQL’s DATE_FORMAT for Postgres’s TO_CHAR, translated the format codes from %Y-%m to YYYY-MM, and confirmed that LIMIT, the WHERE clause and the alias all work unchanged. For anyone moving between MySQL and Postgres, that is a genuinely useful few seconds, and it explains the change rather than just handing you a new query to paste blind.
The rule with SQL
One rule I would not break. Read the query before you run it, and never run a generated write or delete against real data without checking it first. ChatGPT will hand you a confident answer even when your request was ambiguous, so the judgement stays yours.
For turning query results into insight, see how to use ChatGPT for data analysis. For getting sharper answers out of any prompt, see how to write better AI prompts.
FAQ
Can ChatGPT write correct SQL?+
In my test, yes, and it was tidy about it, even using a half-open date range to avoid an end-of-month bug. The catch is ambiguity: give it a vague ask and it will assume a definition rather than ask, so define what you mean and read its stated assumptions.
Do I need to give it my schema?+
It helps enormously. Hand it the table and column names and exactly what you want back, and the query it writes will actually fit your database instead of an imagined one.
Is the free tier enough for SQL?+
For drafting, explaining and translating queries, yes. I ran this whole test on the free tier. Paid plans mainly add higher limits and smarter modes.
Can it convert between MySQL and PostgreSQL?+
It did it correctly here, swapping DATE_FORMAT for TO_CHAR and translating the format codes, and it explained each difference. Still run and check the result, especially on anything non-trivial.



