Back to Prompt Library
Data Analysis
SQLreportingdata analysisbusiness intelligencequeries

SQL Report Builder

A clean, commented SQL report query with CTEs, performance notes, and a plain-English walkthrough of the logic.

Best For

  • A data analyst turning a stakeholder's plain-English report request into a commented, CTE-structured SQL query ready to run in Snowflake
  • An engineer documenting a recurring revenue-by-region report so any teammate can read and modify the query later
  • An analytics consultant flagging ambiguous requirements in the schema before writing a query that might return the wrong numbers

Prompt Template

Write a SQL query (or set of queries) to produce the following report: [REPORT_DESCRIPTION]. Database: [DATABASE_TYPE, e.g., PostgreSQL, BigQuery, Snowflake, MySQL]. Available tables and their relevant columns: [TABLE_SCHEMA_DESCRIPTION]. Report requirements: (1) Metrics to calculate: [METRICS_LIST], (2) Dimensions to group by: [DIMENSIONS], (3) Filters to apply: [FILTERS, e.g., date range, active users only, specific regions], (4) Sort order: [SORT_ORDER], (5) Output format: [OUTPUT_FORMAT, e.g., one row per day per segment / one summary row per user]. Write the query with: (a) clear CTE (Common Table Expression) structure for readability, break complex logic into named steps, (b) inline comments on any non-obvious logic, (c) a note on expected query performance and any indexes that should exist for efficiency. After the query, provide a plain-English explanation of what each CTE or major section does. If any requirement is ambiguous or the schema appears to be missing data, flag it with a [QUESTION: ...] comment in the code.

Pro Tip

Require the model to flag ambiguous requirements with inline [QUESTION:] comments rather than guessing, since silently assumed filters are the most common source of wrong numbers in stakeholder reports.

Example Output

A sample of what this prompt produces once you fill in the placeholders.

WITH monthly_orders AS (SELECT date_trunc('month', order_date) AS month, region, SUM(total) AS revenue FROM orders WHERE status = 'completed' GROUP BY 1, 2) SELECT month, region, revenue FROM monthly_orders ORDER BY month DESC, revenue DESC. Plain-English note: the CTE aggregates completed orders by month and region before the final query sorts by most recent month and highest revenue.

How to use this prompt

  1. Copy the prompt template using the button above.
  2. Paste it into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.).
  3. Replace all bracketed placeholders like [TOPIC] with your specific details.
  4. Send the prompt and refine the output as needed.