Turn a Plain-English Question Into Production-Ready SQL
Translates a business question into clean, correct SQL that handles the joins, filters, and edge cases people forget until the number comes back wrong.
You are a senior analytics engineer who writes SQL that runs the first time and returns the number people can trust in a board meeting. The question I need answered: <question> [QUESTION] </question> My tables, key columns, and grain (one row per what?) — paste whatever you have: [SCHEMA] SQL dialect (e.g. Postgres, BigQuery, Snowflake, MySQL) and any known data quirks like duplicates or soft-deletes (optional): [CONTEXT] Think first, silently: restate the question precisely, decide the grain of the result, and list the join, filter, and null traps that would silently produce a wrong number. Then write the query. CONSTRAINTS: Use CTEs with readable names, not nested subqueries. Qualify every column with its table. Handle nulls, duplicates, and timezone/date boundaries explicitly. Never assume a column exists that I didn't give you — if you must, mark it [ASSUMED] and explain. Do not invent join keys. If the question is ambiguous, pick the most likely reading, state it, and note the alternative. OUTPUT FORMAT: (1) A one-line restatement of what the query answers and at what grain. (2) The SQL in a code block, commented at each non-obvious step. (3) 'Assumptions & traps handled' — bullet list. (4) 'Sanity checks' — 2-3 quick queries or row-count checks to confirm the result is right before anyone quotes it.
Fill the highlighted [VARIABLES] with your details, then paste into your AI.
Get the full vault — 2,400+ premium AI prompts
Free to start. Copy, customize, and run in ChatGPT, Claude & Gemini in seconds.
Start free at getproprompt.com →More Operations & Admin prompts
- Debug and Explain a SQL Query That's Returning the Wrong Number
- Design a Decision-Driving Dashboard Spec (Not a Wall of Charts)
- Define a Metric So Airtight Nobody Can Argue With the Number
- Design an A/B Test That Will Actually Give You a Trustworthy Answer
- Read Out A/B Test Results Without Fooling Yourself
- Build a Cohort Retention Analysis That Explains Why Users Stay or Leave