Gemini prompt to write a SQL query
A copy-paste prompt that produces a correct, readable SQL query from a plain-language description of what you need.
Tuned for Gemini · structured role / context / task / output
Open in the builder →Ready-to-use prompt
# Role You are a senior software engineer who writes clear, idiomatic, production-quality code. # Context Write a query that returns the top 10 customers by revenue this quarter. # Task Write the code described in the context above. Make it correct and idiomatic, handle the obvious edge cases, and briefly note any assumptions inline. # How to approach this Be precise and literal about the requirements. Where a claim depends on facts, briefly note the reasoning or source behind it. # Constraints - Prefer structured output — tables, numbered steps, or labeled sections — over long paragraphs where it aids clarity. - Flag anything in the context that is factually uncertain rather than asserting it confidently. - Cite a source only if you can identify it by full title (and year for papers). Never fabricate a URL. - Maintain consistent formatting across long outputs — if you start with H2 headings or bullets, don't drift to a different structure mid-document. - Do not include apologies or meta-commentary about the prompt itself. # Output format Return the code in a single fenced code block. Brief inline comments only for non-obvious decisions.
Customize this prompt
Answer a few quick questions and Promptivo re-tunes the prompt above to your exact situation.
Start the interview →Why this prompt works
SQL is precise, so the prompt forces the schema/table context into the Context block — without it the model invents column names.
Asking for the dialect (Postgres, MySQL, BigQuery) is included because functions and syntax diverge and a generic query often won't run.
Requesting a one-line explanation of the query as output catches logic errors the user can spot before running it.
Tuned for Gemini — and how it differs
| Model | What Promptivo changes |
|---|---|
| ChatGPT | Concise instructions, strict format adherence, no preamble. |
| Claude | Explicit step-by-step thinking and clearly delimited output sections. |
| Gemini | Precise phrasing with brief reasoning cues where they improve accuracy. |
Questions, answered
- What should a SQL prompt always include?
- The dialect, the relevant table and column names, and the exact result you want. Promptivo's interview captures these so the query runs the first time.