Back to prompts
Data AnalysisChatGPTClaude

SQL Query Writer for Business Reports

Generate SQL queries for common business reporting needs — revenue trends, cohort analysis, funnel metrics, and more.

Prompt Template

You are a data engineer specializing in business analytics. Write SQL queries for the following report:

Database: [PostgreSQL / MySQL / BigQuery / Snowflake]
Report type: [e.g., monthly revenue by product, cohort retention, funnel conversion]
Tables available: [list tables and key columns]
Time period: [e.g., last 90 days, year-to-date]
Filters needed: [e.g., only US customers, exclude test accounts]
Grouping: [e.g., by week, by region, by plan tier]
Metrics needed: [specific numbers you want]

Provide:
1. The SQL query (with comments explaining key sections)
2. How to interpret the output
3. Any indexes that would speed up this query
4. A variation for month-over-month comparison

Example Output

Monthly Revenue by Product Tier — PostgreSQL

SELECT

DATE_TRUNC('month', o.created_at) AS month,

p.tier,

COUNT(DISTINCT o.customer_id) AS paying_customers,

SUM(o.amount_cents) / 100.0 AS revenue_usd,

ROUND(AVG(o.amount_cents) / 100.0, 2) AS avg_order_value

FROM orders o

JOIN products p ON p.id = o.product_id

WHERE

o.status = 'completed'

AND o.created_at >= DATE_TRUNC('year', CURRENT_DATE) -- YTD

AND o.test_mode = false -- exclude test accounts

GROUP BY 1, 2

ORDER BY 1 DESC, revenue_usd DESC;

**Interpretation:** Each row = one month + product tier combination. Compare `revenue_usd` across tiers to spot which tier drives growth.

Index recommendation:

CREATE INDEX idx_orders_created_status ON orders(created_at, status) WHERE test_mode = false;

MoM Comparison variation:

-- Add LAG() window function to compare with prior month

LAG(revenue_usd) OVER (PARTITION BY tier ORDER BY month) AS prev_month_revenue

Tips for Best Results

  • 💡Share your actual table and column names — generic column names like 'amount' vs 'amount_cents' cause bugs
  • 💡Ask for the query and then ask 'what edge cases could cause incorrect results?' — it catches subtle issues
  • 💡Always add `LIMIT 100` while testing, then remove it for the full run

Try it with