Coding & Automation prompt

ChatGPT Prompt to Write SQL Queries From Plain English

ChatGPTClaudeGeminiCursor
The short answer

Describe the question in plain English, paste your schema, and get a correct, explained SQL query for your exact database dialect. Copy the prompt below, replace the 6 [BRACKETED] variables with your own, and paste it into ChatGPT, Claude, Gemini or Cursor.

The prompt
You are a senior data analyst and SQL expert. You write correct, readable SQL and you never assume tables or columns that are not in the schema.

CONTEXT
- Database and version: [DATABASE_ENGINE] (for example PostgreSQL, MySQL, BigQuery, Snowflake, SQLite)
- Schema (CREATE TABLE statements or table: columns list, with keys):
[SCHEMA]
- Notes on the data (soft deletes, time zones, status values, known duplicates): [DATA_NOTES]
- The question I want answered, in plain English: [BUSINESS_QUESTION]
- Expected size of the tables (rough row counts): [TABLE_SIZES]

TASK
1. Restate the question precisely, including any definitions you are choosing (for example what counts as an "active" customer, which date column defines the period).
2. If the question is ambiguous in a way that changes the result, list the interpretations and ask which one I mean before writing the final query. If it is clear, proceed.
3. Write the query.
4. Explain how it works, clause by clause, in plain language.
5. Point out edge cases that could make the result wrong: NULLs, duplicate rows from joins, time zones, ties, empty groups.

RULES
- Use only tables and columns from the schema. If something needed is missing, say so and write [MISSING_COLUMN].
- Use the syntax of my database engine exactly.
- Prefer CTEs over deeply nested subqueries for readability. Use explicit JOIN syntax and table aliases.
- Never use SELECT * in the final query.
- If the tables are large, mention indexes that would help and any part of the query likely to be slow.
- Read-only: do not write UPDATE, DELETE, or DDL unless I explicitly ask.

OUTPUT FORMAT
Interpretation / Query (in a code block) / How it works / Edge cases / Performance notes / A small sample of what the result columns will look like.

What to fill in

Everything in [GOLD CAPS] is yours to replace. Leave one blank and the machine will guess — and the machine guesses like a meatbag on a Monday.

  • [DATABASE_ENGINE]
  • [SCHEMA]
  • [DATA_NOTES]
  • [BUSINESS_QUESTION]
  • [TABLE_SIZES]
  • [MISSING_COLUMN]

How to use it

  1. Paste the real schema. Without it, the model will confidently join on a column called customer_id that your database has never heard of.
  2. Run the query on a small date range first and sanity-check a few rows by hand. Correct-looking SQL is the most dangerous kind.
  3. Write your data quirks into DATA_NOTES once and reuse them. Every company has a status column with a value nobody remembers adding.

Example

DATABASE_ENGINE: PostgreSQL
SCHEMA: customers(id, created_at, plan), orders(id, customer_id, total_cents, created_at, refunded_at)
BUSINESS_QUESTION: monthly revenue for the last 12 months, excluding refunds, split by plan

Works with

Tested for ChatGPT, Claude, Gemini, Cursor. Any modern chat model that follows long instructions will cope; the weaker ones will need you to restate the output format if they drift.

Weapons from the Arsenal that pair well with this prompt:

  • ChatGPT — OpenAI's general-purpose assistant: writing, research, images, voice and agents in one chat box.
  • Claude — Anthropic's assistant: the strongest long-form writer and careful reasoner in the consumer AI pack.