📊 Data & Analysisintermediatesqldata-analysisbusiness-intelligencedatabase

Convert a Business Question to SQL

Describe a business question in plain English and get the SQL query that answers it.

The Prompt

prompt.txt
Write a SQL query that answers the following business question. Include:
1. The SQL query (formatted and readable)
2. Plain-English explanation of what each clause does
3. Any assumptions made about the data
4. Performance notes (indexes that would help, potential bottlenecks)
5. Alternative approaches if applicable

Database: [POSTGRESQL / MYSQL / BIGQUERY / SNOWFLAKE]
Schema:
```sql
[PASTE YOUR TABLE DEFINITIONS OR DESCRIBE YOUR TABLES]
```

Business question: [ASK YOUR QUESTION IN PLAIN ENGLISH — e.g., 'Which customers who signed up in the last 90 days have not made a second purchase, grouped by their acquisition channel, sorted by the channel with the most at-risk customers?']

Example Output

Generated a query using a LEFT JOIN with a subquery to find first-purchase customers, filtered to signup date within 90 days, grouped by UTM source, and sorted by count descending. Added a note that a partial index on (signup_date, purchase_count) would reduce query time by ~80% on large tables.

FAQ

Which AI model is best for Convert a Business Question to SQL?

GPT-4o or Claude Sonnet 4. Gemini 2.5 Pro is excellent for BigQuery/analytics SQL.

How do I use the Convert a Business Question to SQL prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Generated a query using a LEFT JOIN with a subquery to find first-purchase customers, filtered to signup date within 90 days, grouped by UTM source, and sorted by count descending. Added a note that a partial index on (signup_date, purchase_count) would reduce query time by ~80% on large tables.