Write a SQL Query From a Business Question
Translate a business question into a correct, readable SQL query for your actual schema, with assumptions and checks spelled out.
Make it yours
Fill in each variable and watch the prompt update. Anything still in {{braces}} needs your input before you use it.
Fill in the blanks
0 of 3 filledYour database engine — syntax differs.
Relevant tables and columns, with types if possible.
What you want to know, in plain language.
You are a careful data analyst who writes clear, correct SQL. Database: {{sql_dialect}} Schema (tables and columns): {{schema}} Business question: {{question}} Requirements: - Use only the tables and columns listed above. If the question can't be answered with them, say what's missing instead of inventing columns. - Write readable SQL: CTEs for multi-step logic, meaningful aliases, and a comment above each step. - State every assumption (e.g. how you define "active", how you handle NULLs, time zones, or duplicates). - After the query, explain in 3–5 bullets what it does, then suggest 1–2 sanity-check queries I can run to verify the result.
Example
Example input
You are a careful data analyst who writes clear, correct SQL. Database: PostgreSQL Schema (tables and columns): customers(id, name, signup_date, country) orders(id, customer_id, order_date, total_amount, status) Business question: Which 10 countries had the highest average order value from completed orders in the last 90 days, counting only countries with at least 20 such orders? Requirements: - Use only the tables and columns listed above. If the question can't be answered with them, say what's missing instead of inventing columns. - Write readable SQL: CTEs for multi-step logic, meaningful aliases, and a comment above each step. - State every assumption (e.g. how you define "active", how you handle NULLs, time zones, or duplicates). - After the query, explain in 3–5 bullets what it does, then suggest 1–2 sanity-check queries I can run to verify the result.
Example output
-- Step 1: completed orders from the last 90 days, with the customer's country
WITH recent_orders AS (
SELECT o.id, o.total_amount, c.country
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'completed'
AND o.order_date >= CURRENT_DATE - INTERVAL '90 days'
)
-- Step 2: average per country, keeping countries with 20+ orders
SELECT country,
COUNT(*) AS order_count,
ROUND(AVG(total_amount), 2) AS avg_order_value
FROM recent_orders
GROUP BY country
HAVING COUNT(*) >= 20
ORDER BY avg_order_value DESC
LIMIT 10;
What it does
- Joins orders to customers to get each order's country
- Keeps only completed orders from the last 90 days
- Averages order value per country and drops countries with fewer than 20 orders
- Returns the top 10 by average
Assumptions
- Completed orders have status exactly 'completed' (check: SELECT DISTINCT status FROM orders;)
- Orders with a NULL country form their own group; filter them out if unwanted
Sanity checks
- Remove the LIMIT and HAVING to see all countries and their counts
- Spot-check one country: SELECT AVG(total_amount) for its completed orders in the same window
Why it works
Pasting the real schema is the single biggest improvement for SQL prompts — it stops the model from inventing plausible-sounding columns. Requiring stated assumptions exposes the hidden decisions (what counts as "completed", how NULLs are treated) that make queries silently wrong, and the sanity-check queries give you a way to verify the result rather than trusting it.
Techniques used
- Contextual promptingGive the AI the background it can't guess — who you are, your goal, your audience, your constraints — so answers fit your real situation.Learn the technique
- Instruction promptingWrite clear, explicit, positive instructions — action, scope, constraints, format — so the AI knows exactly what done looks like.Learn the technique
- Role promptingAsk the AI to respond as a specific role or expert to shape its focus, vocabulary, and tone for your audience.Learn the technique