Ready-to-use AI prompts for write and debug sql — written for Data Analyst and easy to paste into ChatGPT, Claude or Gemini.
When you need write and debug sql done, these data analyst prompts give you a fill-in-the-blanks starting point covering sql, query, database. Fill in fields like dialect, schema, data quirks and paste the result into ChatGPT, Claude, or Gemini.
Write a query from a business question
You are a senior analytics engineer writing [dialect] SQL. Here is my schema:
<schema>
[schema]
</schema>
Known data quirks: [data quirks].
Business question: [question].
Before writing SQL, restate the question as a precise definition — what counts as the entity, the time window, and any exclusions. If my question is ambiguous, state the interpretation you are using.
Then write the query with CTEs rather than nested subqueries, a comment per CTE, and explicit handling of nulls and duplicates. Finish with a sanity check I can run to confirm the result is plausible.
Debug or optimize a query
Act as a database performance specialist. Here is my query and what it does wrong:
<query>
[query]
</query>
Problem: [problem]. Dialect: [dialect]. Table sizes: [table sizes]. Indexes: [indexes]. Query plan if available: [query plan].
If the results are wrong, trace the logic step by step and find where rows are being duplicated, dropped by an inner join, or filtered by a WHERE clause that should be in the ON clause. If it is slow, identify the specific operation causing it — full scan, spilled join, function on an indexed column, or an unnecessary DISTINCT.
Give the corrected query, a diff-style explanation of each change, and how to verify the new version returns the same intended rows.
Explain someone else's SQL
You are a data engineer explaining inherited code. Here is a query I need to modify:
<query>
[query]
</query>
Schema context: [schema context]. What I need to change: [goal].
Explain: what business question this query answers, what each CTE or subquery contributes, the grain of the output (one row per what?), every filter and what it excludes, and any join that could silently drop or duplicate rows.
Then tell me specifically where to make my change and what else it would affect. Flag anything in the query that looks like a bug or a stale assumption — hardcoded dates, magic IDs, filters that no longer make sense — but do not change them without telling me.
How to use this prompt
Fill in the blanks. Type your details into each highlighted field of this write and debug sql prompt — fields like dialect, schema, data quirks — and it rewrites itself live.
Copy the prompt. Press Copy prompt to put the finished write and debug sql prompt on your clipboard.
Paste into your AI model. Paste it into ChatGPT, Claude, Gemini, or any chat LLM to get your write and debug sql result.
Review and iterate. Read the response as a data analyst would, then adjust fields like dialect, schema, data quirks and re-run to sharpen it.
Fill-in fields explained
dialect
e.g. BigQuery / Postgres / Snowflake
schema
paste table names, columns, types, keys, and how tables relate
data quirks
e.g. soft deletes, duplicate rows from a join, timestamps in UTC, test accounts to exclude
question
Your question.
query
paste SQL
problem
wrong results / too slow / times out
table sizes
row counts
indexes
Your indexes.
query plan
paste if available
schema context
whatever you know
goal
what you need to change
Frequently asked
Which AI models does the Write and Debug SQL prompt work with?
The Write and Debug SQL prompts for Data Analyst are plain-text and model-agnostic — paste them into ChatGPT (GPT-4o), Claude, Gemini, Llama, or any other chat model.
How do I customize the Write and Debug SQL prompt for my own use?
Fill in the 11 labelled fields (dialect, schema, data quirks, …) before copying, or edit the [bracketed] text after you paste.
Are the Write and Debug SQL prompts for Data Analyst free to use?
Yes. Every Write and Debug SQL prompt here is free to copy and reuse, including for commercial and client work, with no sign-up.