Asset 20 8 2
Does AI recommend your business? Run the free check →

Join 15,000 business owners, marketers and entrepreneurs. The Sunday newsletter you'll be annoyed only arrives once a week.

Article

ChatGPT Prompts for SQL: Write, Fix and Explain Queries

The best ChatGPT prompts for SQL name your database (PostgreSQL, MySQL, SQL Server, SQLite), paste the table structure and describe the result you want in plain words. Then ask it to explain the query. It's a great query writer, but it can't see your live database: OpenAI says its analysis environment can't make external requests.

I learned SQL the slow way, one error message at a time. ChatGPT makes that far quicker: describe what you want, give it your table structure and it'll write the query, explain it and fix it when it breaks. The trap is that it writes plausible SQL for a database it's never seen. These prompts give it the context it needs and ask it to show its reasoning, so you can check before you run anything.

What to know before you trust ChatGPT's SQL

PointWhy it matters
SQL dialects differ (PostgreSQL, MySQL, SQL Server, SQLite)Always name yours, or you'll get syntax that doesn't run
OpenAI says ChatGPT's data analysis environment can't make external web requests or API callsIt can't connect to your database; paste the schema or upload a sample instead
The PostgreSQL docs say LIMIT without ORDER BY returns an unpredictable subset of rowsAsk for an ORDER BY whenever you want the top or first rows
PostgreSQL skips OFFSET rows before counting the LIMIT rowsCheck paging queries carefully
OpenAI says ChatGPT can produce incorrect outputTest every query on a copy or with a read-only account first

The LIMIT point is the one that bites people. A query that 'gets the latest ten orders' without an ORDER BY can return a different ten each time, and it'll look fine in testing.

Prompts to write a query

Give the dialect, the tables and the result you want.

Copy this prompt

I'm using [PostgreSQL / MySQL / SQL Server / SQLite]. Here are my tables: [paste CREATE TABLE statements or column lists]. Write a query that returns [result], and explain each part.

Copy this prompt

Write a query that lists each [customer] with the number of [orders] they've placed, including customers with none, sorted from most to fewest.

Copy this prompt

Write a query to find duplicate rows in [table] based on [columns], showing how many times each duplicate appears.

Copy this prompt

Return the most recent [record] for each [group] from [table], using a method that works in [dialect].

Copy this prompt

Write a query that shows the total of [column] by month for [date range], with months that have no data shown as zero.

Prompts to understand and fix queries

When you've inherited SQL or it's throwing errors.

Copy this prompt

Explain this query line by line in plain English, and tell me what result it returns: [paste query].

Copy this prompt

This query gives the error [paste error] in [dialect]. Explain the cause and give me a corrected version: [paste query].

Copy this prompt

This query returns more rows than I expect. Here's the query and a description of the tables: [paste]. What's causing the duplicates?

Copy this prompt

Rewrite this query so it's easier to read, using clear aliases and common table expressions, without changing the result: [paste].

Prompts for performance

Slow queries are where a second pair of eyes helps.

Copy this prompt

This query is slow on a table with many rows: [paste query and table structure]. Suggest indexes and rewrites, and explain the trade-offs.

Copy this prompt

Here's the query plan output from [dialect]: [paste]. Explain in plain language where the time is going.

Copy this prompt

Is there a way to write this query without a subquery in the SELECT list? Show me and explain which is likely to be faster: [paste].

Prompts for learning SQL

For beginners and anyone switching dialects.

Copy this prompt

Work with me

Want AI doing the heavy lifting in your marketing?

I build the systems that handle the boring 80 percent, so you get your week back. Done properly, with the human kept in.

Teach me JOINs using two small example tables you invent. Show the result of each JOIN type and quiz me afterwards.

Copy this prompt

Give me ten practice SQL questions of increasing difficulty using one example schema, with answers hidden at the end.

Copy this prompt

I know [dialect A]. What are the main syntax differences I'll hit in [dialect B], with examples?

Copy this prompt

Explain GROUP BY and HAVING with a simple example, then give me three exercises.

Prompts for safer changes

UPDATE and DELETE deserve extra care.

Copy this prompt

Write this UPDATE as a SELECT first so I can check which rows it will change: [describe change].

Copy this prompt

Wrap this change in a transaction with a way to check the result before committing, in [dialect]: [paste statement].

Copy this prompt

Review this DELETE statement and tell me if it could remove more rows than intended: [paste].

Copy this prompt

Write a query to back up the rows I'm about to change into a new table first.

Prompts for reporting queries

For the weekly numbers someone always asks you for.

Copy this prompt

Write a query for a weekly report showing new [records] per week for the last eight weeks, with the week start date in the first column.

Copy this prompt

Turn this query into a view called [name] so the team can reuse it, and explain how to grant read-only access in [dialect]: [paste query].

Copy this prompt

Write a query comparing this period with the previous one for [metric], showing both values and the difference side by side.

How to get better SQL from ChatGPT

  • Name your database and version in every prompt.
  • Paste the real table structure and a few example rows with sensitive data removed.
  • Describe the result you want, including how to handle blanks and ties.
  • Ask it to explain the query, then test it on a copy before running it on live data.
  • Use ORDER BY whenever you use LIMIT, as the PostgreSQL docs advise.

Before pasting real data into any tool, it's worth knowing the risks of SQL injection and how parameterised queries prevent it. If you're moving data between systems, my explainer on database migration services covers the bigger picture, and Claude for Excel helps if your data lives in spreadsheets.

My advice on ChatGPT prompts for SQL

Use ChatGPT to write the first draft of a query, to explain unfamiliar SQL and to untangle error messages. Give it your dialect and schema every time, because it can't see your database. And build one habit: run anything that changes data as a SELECT first, on a copy if you can. ChatGPT will make you much faster at SQL. Testing is what keeps you safe.

Frequently asked questions

Can ChatGPT write SQL queries?

Yes. Give it your database type, table structure and the result you want, and it can write and explain the query. Test it before running it on live data.

Can ChatGPT connect to my database?

Not through its data analysis environment. OpenAI says that environment can't make external web requests or API calls, so paste the schema or upload sample data.

Why does my LIMIT query return different rows?

The PostgreSQL documentation says LIMIT without an ORDER BY returns an unpredictable subset of rows. Add an ORDER BY that gives a unique order.

Which SQL dialect should I tell ChatGPT?

The one your database uses, such as PostgreSQL, MySQL, SQL Server or SQLite, because syntax differs between them.

Is it safe to paste data into ChatGPT?

Remove personal or confidential data first, and follow your company's policy on AI tools. Table structures and dummy rows are usually enough.

Published and maintained by the Lilach Bullock team, covering marketing, AI and business growth.
Your buyers are asking AI who to use. Does it say you?

See for free whether ChatGPT, Claude, Perplexity, Gemini and Google name you, and get the plan to become the answer.

Check my AI visibility →
Sundays only

Get the Sunday newsletter.

One email a week. AI experiments, marketing tactics, and the workflows Lilach is building right now in her own business.

Subscribe free

Let’s get your marketing running on AI.

Book a free 30-minute call

We figure out what you need, where AI fits in, and what working together would look like.

Book the call →

Or take the 30-second calculator

You’ll see the hours and the money quietly leaking out of your week, and the three workflows worth building first.

Take the calculator →

Or grab the free AI resource library

Prompt packs, templates, checklists, and swipe files. The exact tools I build for paying clients. Yours, free.

Get the library →
Keep reading

More from the blog.