SQL query from a plain-English question
A query with its assumptions stated, so you can check them before running it. Written for Claude, free to copy, with every placeholder explained below.
The prompt
<role>
You write SQL that is correct before it is clever.
</role>
<schema>
{{schema}}
</schema>
<context>
Database: {{database}}
Approximate row counts: {{row_counts}}
</context>
<task>
Write a query that answers: {{question}}
</task>
<constraints>
- Use only tables and columns present in the schema above. If something needed is
missing, say so instead of inventing a column name.
- State every assumption you made about the data before the query.
- Handle NULLs explicitly wherever a join or aggregate could produce them.
- Prefer a readable query over a marginally faster one, unless the row counts make
it matter — in which case say why.
- Comment any non-obvious join or window function.
</constraints>
<output_format>
1. Assumptions.
2. The query.
3. What it returns, described in one sentence.
4. Anything that would change the result if my assumptions are wrong.
</output_format>
Now write the query.Open this in the prompt builder to adapt it, score it, and re-render it for a different model.
What to replace
| Placeholder | What to put there |
|---|---|
| {{schema}} | CREATE TABLE statements or a column listing. |
| {{database}} | PostgreSQL, MySQL, SQLite — the dialects differ. |
| {{row_counts}} | Rough table sizes, so performance advice is grounded. |
| {{question}} | The question in plain English. |
Why this prompt works
- Assumptions stated before the query
- Most wrong SQL is right code applied to a wrong assumption about the data. Surfacing them makes the error checkable without running anything.
- Invented columns forbidden
- Plausible-looking column names are the most common hallucination in generated SQL, and they fail at runtime rather than at review.
- Explicit NULL handling
- Silent NULL propagation through joins and aggregates is the classic source of quietly wrong numbers.
More prompts
- Code review for correctness only — Findings with a triggering input, ranked by severity — and permission to find nothing.
- Debug from an error and stack trace — Ranked hypotheses with a test for each, instead of one confident guess.
- Unit tests written from the spec — Tests derived from requirements rather than from the implementation, so they can fail.
- Explain unfamiliar code — A walkthrough pitched at your actual level, with the non-obvious decisions called out.
- Blog post from an outline — Turn a rough outline into a full draft that keeps your structure and does not pad.
- Rewrite for clarity without changing meaning — Tighten prose while keeping the author's voice and every factual claim intact.
Browse the full prompt library, all coding & development prompts, or read the guides.