Write a SQL Query From a Business Question
Translate a business question into a clean, commented SQL query against your schema.
This prompt translates a plain-English business question into a runnable, well-formatted SQL query grounded in your actual schema. You give it the database flavor and your table definitions so it uses the correct dialect and real column names instead of inventing them. The request for inline comments and an explicit list of assumptions is what makes it safe to use: you can audit the logic and catch any guesses the model made about joins or business rules before you run it on production data.
I have a [DATABASE TYPE: e.g. Postgres / MySQL / BigQuery] database with these tables: [PASTE SCHEMA OR TABLE DEFINITIONS] Write a SQL query to answer: '[BUSINESS QUESTION]'. Format the query for readability with line breaks and comments explaining each non-obvious clause. Note any assumptions you made about the schema or business logic.
What you can expect back
SELECT -- Truncate order timestamp to the first day of its month DATE_TRUNC(DATE(o.created_at), MONTH) AS order_month, -- total_cents stored as integer cents; convert to dollars SUM(o.total_cents) / 100 AS revenue_usd FROM orders AS o JOIN customers AS c ON o.customer_id = c.id WHERE -- Only customers who signed up during calendar year 2025 c.signup_date BETWEEN '2025-01-01' AND '2025-12-31' -- Assuming 'completed' is the status for fulfilled orders AND o.status = 'completed' GROUP BY order_month ORDER BY order_month; Assumptions: - 'Revenue' counts only orders with status = 'completed'. - total_cents is in cents; divided by 100 for dollars. - Customer signup year is based on signup_date, not first order date.
Illustrative example — your results will vary by tool and inputs.
Get sharper results
- 01Always paste real column names and types rather than describing tables in prose, because the model can only avoid hallucinated fields if it sees the actual schema.
- 02Read the 'assumptions' section first and confirm each one, since a wrong guess about a status value or join key produces a query that runs but returns wrong numbers.
- 03If a metric is ambiguous (revenue gross vs. net, active users by login vs. order), define it inside the business question so the model doesn't have to guess.
- 04Ask it to add a LIMIT 100 and run the query as a sanity check before removing the limit for the full aggregation.
- 05For expensive engines like BigQuery, request that it select only needed columns and filter on partitioned date fields to keep scan cost down.
Adapt it for your case
Paste your current slow query instead of a question and ask the model to rewrite it for performance while preserving identical results.
Ask it to wrap the answer in a CREATE VIEW or a parameterized CTE so the logic can be reused across dashboards.
Flip the task: paste an unfamiliar SQL query and ask for a plain-English description of what business question it answers and any risky assumptions.
Common questions
Can I trust the query without reviewing it?
No. Always read the comments and assumptions and test on a small sample first, because the model can misinterpret column meaning or business rules even with the schema provided.
What if I don't have the full schema handy?
Provide as much as you have and the model will note assumptions for missing pieces, but accuracy improves significantly with complete table and type definitions.
Will it work across different SQL databases?
Specify the exact engine in the prompt, since functions like date truncation, string handling, and pagination differ between Postgres, MySQL, and BigQuery.
You may also need
Analyze a Dataset With Pandas Step-by-Step
Generate step-by-step Pandas EDA code covering nulls, outliers, and a business question.
Write a Data Cleaning Script for Messy Data
Generate a step-by-step Pandas data cleaning script with issue detection and before/after summaries.
Use SQL Window Functions for Advanced Analytics
Generate advanced SQL window function queries with explanations and performance notes.
Turn a Plain-English Question Into a SQL Query
Converts a natural-language business question into a correct, commented SQL query grounded in your real schema.