AI prompt for writing a SQL query
This is a ready-made AI prompt for writing a SQL query, written for Claude and free to copy. Most wrong SQL is correct code applied to a wrong assumption about the data. The second failure is invented column names, which look right and fail at runtime rather than at review. The prompt below handles that: it is compiled as a coding task, so it carries the structure and the constraints that kind of work needs.
The prompt
Written for Claude. Compiled as a coding task — writing, reviewing, debugging, or refactoring software.
<role>
You are a senior software engineer who writes production-ready, tested code.
</role>
<context>
Database: {{database}}. Schema: {{schema}}. Approximate row counts: {{row_counts}}.
</context>
<task>
Write a SQL query that answers: {{question}}.
</task>
<constraints>
- Use only tables and columns present in the schema. If something needed is missing, say so.
- State every assumption about the data before the query.
- Handle NULLs explicitly wherever a join or aggregate could produce them.
- Comment any non-obvious join or window function.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
</constraints>
<output_format>
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
</output_format>
<approach>
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
</approach>
<success_criteria>
Before you finish, check the output against every item below and fix anything that fails.
- A reviewer could verify correctness from the stated assumptions alone.
</success_criteria>
Now, write a SQL query that answers: {{question}}.Optional: start the reply with ``` to force the output straight into shape.
Open this in the prompt builder to add your own context and see what would improve it most.
What makes writing a SQL query hard to prompt for
Most wrong SQL is correct code applied to a wrong assumption about the data. The second failure is invented column names, which look right and fail at runtime rather than at review.
Why this prompt works
- Background comes before the instruction
- The model reads the situation before it learns what to do with it, which stops the instruction being diluted by everything that follows. The task is then restated as the final line, where models weight it most heavily.
- Rules a coding task assumes but nobody writes down
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections. Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle. These are added automatically because leaving them implicit is the most common reason this kind of output disappoints.
- Success criteria the model checks itself against
- Stating how the output will be judged gives the model something concrete to verify before it finishes. Prompts without criteria produce work that is plausible but incomplete, because nothing defined when it was done.
- A prefill that removes the preamble
- Starting the reply with ````` forces the output straight into the required shape and removes the "Sure, here is..." opener entirely.
What to replace
| Placeholder | What to put there |
|---|---|
| {{database}} | Your database. |
| {{schema}} | Your schema. |
| {{row_counts}} | Your row counts. |
| {{question}} | Your question. |
Check the output before you use it
- Verify every column referenced actually exists in your schema.
- Read the stated assumptions — this is where the error usually is, not in the syntax.
- Run it against a copy first if it modifies anything.
The same prompt for other models
Identical content, packaged the way each model reads most reliably.
ChatGPT
## Role
You are a senior software engineer who writes production-ready, tested code.
## Context
Database: {{database}}. Schema: {{schema}}. Approximate row counts: {{row_counts}}.
## Task
Write a SQL query that answers: {{question}}.
## Constraints
- Use only tables and columns present in the schema. If something needed is missing, say so.
- State every assumption about the data before the query.
- Handle NULLs explicitly wherever a join or aggregate could produce them.
- Comment any non-obvious join or window function.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
## Output format
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
## Approach
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
## Success criteria
Before you finish, check the output against every item below and fix anything that fails.
- A reviewer could verify correctness from the stated assumptions alone.
Now, write a SQL query that answers: {{question}}.Gemini
**Task**
Write a SQL query that answers: {{question}}.
**Role**
You are a senior software engineer who writes production-ready, tested code.
**Context**
Database: {{database}}. Schema: {{schema}}. Approximate row counts: {{row_counts}}.
**Constraints**
- Use only tables and columns present in the schema. If something needed is missing, say so.
- State every assumption about the data before the query.
- Handle NULLs explicitly wherever a join or aggregate could produce them.
- Comment any non-obvious join or window function.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
**Output format**
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
**Approach**
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
**Success criteria**
Before you finish, check the output against every item below and fix anything that fails.
- A reviewer could verify correctness from the stated assumptions alone.
Now, write a SQL query that answers: {{question}}.Common questions
- Why does AI invent column names in SQL?
- Because it is asked for something specific it does not reliably have, and a plausible name is the default output. Supplying the full schema and forbidding invention explicitly removes most of it.
- Should I trust AI-written SQL on production data?
- Read it first, particularly the joins and any WHERE clause on a delete or update. Requiring stated assumptions makes the review fast, because the assumption is usually where the error is.
- Which AI model is best for writing a SQL query?
- All of them handle this; what changes is the packaging. This page shows the same prompt written for Claude, ChatGPT, Gemini. Claude follows XML-delimited structure most reliably, ChatGPT works best with markdown headings, and Gemini prefers the task stated before the material. The content of the prompt is identical in each.
- Can I change this prompt for my own situation?
- Yes, and you should. Replace the placeholders with your own details, then open it in the builder to add context specific to you. The builder scores what you supply and tells you exactly which missing piece would improve it most.
- Why does this prompt include rules I did not ask for?
- Because coding tasks carry requirements that experienced practitioners apply automatically and rarely write down. The compiler adds them so the output does not fail on something obvious. Every added rule is listed on the how it works page.
Related prompts
- AI prompt for reviewing code
- AI prompt for debugging an error
- AI prompt for writing unit tests
- AI prompt for refactoring code
- AI prompt for writing a regular expression
- AI prompt for writing a Dockerfile
Browse all ready-made prompts, the full prompt library, or read the guides.