Back to prompts
Code28.10.2026
Write a SQL Query That Builds a Year End Sales Summary by Month and Product From Your Orders Table
#sql#postgres#mysql#sales report#year end#data analysis#small business
You are a senior data engineer who writes clear, safe SQL for small teams. At the end of the year I need a reliable sales summary by month and by product so I can review the year, plan inventory, and answer questions from my accountant. I want a query I can understand, run, and check myself. Use only the schema I give you. Do not invent table names, column names, or business rules. If something is missing, mark it [NEEDS INPUT]. Database engine and version (PostgreSQL, MySQL, SQLite, SQL Server, BigQuery): [ENGINE] Tables and columns for orders, order items, products, and refunds, with data types: [SCHEMA] How a completed sale is marked (status values, paid flag, fulfilled date): [SALE RULES] How refunds, cancellations, discounts, shipping, and tax are stored: [ADJUSTMENTS] Time zone the business reports in and the time zone timestamps are stored in: [TIME ZONE] Year to report and currency: [YEAR AND CURRENCY] Requirements: 1. Restate the business rules you will apply in plain words before writing any SQL, and list every assumption you had to make. 2. Write one read only query that returns, for each month and product: units sold, gross sales, discounts, refunds, and net sales. Include months with zero sales so the report has all 12 months. 3. Convert timestamps to [TIME ZONE] before grouping by month, and explain why this matters for orders placed near midnight on the last day of a month. 4. Keep tax and shipping out of net sales unless [ADJUSTMENTS] says otherwise, and explain the choice. 5. Use readable names, short comments, and common table expressions instead of deeply nested subqueries. 6. Add a second query that returns yearly totals so I can check that the monthly rows add up. 7. Give 4 sanity checks I can run, such as comparing the total with my payment processor report and looking for orders with no items. 8. Point out indexes that would help if the query is slow, without changing anything in the database. 9. Remind me to run it on a copy or a read only connection first and never to run write statements from this prompt. Deliverables: 1. Business rules and assumptions 2. Monthly sales summary query with comments 3. Yearly totals check query 4. Sanity checks and index notes 5. Short explanation I can share with my accountant
How to use this prompt
- 1Copy the prompt using the button above.
- 2Open your AI tool (ChatGPT, Claude, Midjourney, etc.)
- 3Paste and customize any variables in the prompt.
- 4Generate your result.
Tip
Fill every placeholder before you run the prompt. Keep one clear goal per run, then refine with a follow-up if needed.
When to use this prompt
- •Good for first drafts in Code
- •Keep variables filled so the model has enough context
- •Review AI output before sending or publishing