Sales analysis queries with CTEs and window functions
Writes advanced SQL queries using CTEs and window functions for sales performance analysis.
Create a set of reusable SQL queries that analyze sales by period, product, seller, and region using CTEs (Common Table Expressions) and window functions for rankings and comparisons.
At a glance
Access
Free prompt
Open to copy without upgrading.
Prompt objective
Create a set of reusable SQL queries that analyze sales by period, product, seller, and region using CTEs (Common Table Expressions) and window functions for rankings and comparisons.
Real use case
TechBR Online Store, an electronics marketplace processing 50,000 orders/month stored in PostgreSQL, needs standardized queries for the BI team to answer recurring executive questions about sales performance.
Customize these fields first
Replace the placeholders with your own context before you run the prompt. That usually improves the first output more than adding more instructions later.
Prompt
Write a set of advanced SQL queries for sales analysis at [COMPANY NAME], given the following tables:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`orders\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\` (id, order_date, customer_id, seller_id, total_amount, status, channel)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`order_items\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\` (id, order_id, product_id, quantity, unit_price, discount)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`products\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\` (id, name, category_id, subcategory, cost, sale_price)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`customers\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\` (id, name, city, state, registration_date, segment)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\`sellers\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\` (id, name, team, region, hire_date)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n**Query 1 — Seller ranking with targets:**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nUsing ROW_NUMBER() and DENSE_RANK() OVER(), create a monthly seller ranking with:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Total sales, average ticket, order count\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Comparison vs. target of R$ [AMOUNT]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- % achievement and ranking position\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Variance vs. previous month using LAG()\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n**Query 2 — Customer cohort analysis:**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nUsing CTEs, group customers by first purchase date (acquisition month) and calculate:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Month-over-month retention (month 0 to month 12)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Cumulative revenue per cohort\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Average LTV per cohort\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n**Query 3 — Fast-growing products:**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nUsing window functions, identify the [NUMBER] products with the highest sales acceleration:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- 3-month rolling average of revenue\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- MoM growth rate (SUM() OVER with ROWS BETWEEN)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- % contribution to category total\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n**Query 4 — RFM Analysis (Recency, Frequency, Monetary):**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nSegment customers into 5 tiers for each dimension using NTILE(5):\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Recency: days since last purchase\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Frequency: total orders in the last [MONTHS] months\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Monetary: total spend\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Final classification: Champions, Loyal, At Risk, Lost\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n**Query 5 — Executive dashboard in a single query:**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nChained CTE returning in one query:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Period revenue vs. previous period\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Top 5 products, top 5 sellers, top 5 cities\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Cancellation rate\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n- Average ticket and orders by channel\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\nSQL dialect: [PostgreSQL/MySQL/BigQuery]. Comment each block explaining the logic.
Open directly in an AI — the text is pre-filled:
How to use this prompt
- 1Replace the key placeholders first: COMPANY NAME, AMOUNT, NUMBER, MONTHS.
- 2Replace any bracketed placeholders like [this] with your own context.
- 3Add extra background information when you want more tailored results.
- 4Combine multiple prompts in one conversation when you need a richer output.
- 5Save your best-performing prompts so they are easy to reuse later.
Next best step
Open the guide first, then branch only if you still need more.
A guide for technical builders choosing between prompts, coding workflows, and agent-based implementation.
If this prompt is close but not quite right, generate variants next. If the job is recurring, move into the course library after the guide.
Related prompts
View allDatabase Schema Design for Recurring Subscription Systems
Designs an optimized database schema for a subscription business with SaaS metrics.
Best for
Create a complete relational model for a recurring subscription system, including tables, indexes, constraints, and ready-to-use queries for calculating MRR, churn, and LTV.
Slow Query Optimization with EXPLAIN ANALYZE and Indexes
Diagnoses and optimizes slow SQL queries using execution plans, strategic indexes, and query rewriting.
Best for
Learn to identify SQL query performance bottlenecks using EXPLAIN ANALYZE, create efficient indexes, and rewrite queries to reduce response time.
Stored Procedures for Automatic Sales Commission Calculation
Creates stored procedures that automate commission calculations with complex tiered rules and bonuses.
Best for
Develop SQL stored procedures that calculate sales commissions considering tiered quotas, product mix bonuses, cancellation clawbacks, and team splits.
BigQuery Queries for E-commerce Conversion Funnel and Attribution Analysis
BigQuery queries to analyze conversion funnels, channel attribution, and browsing behavior in e-commerce.
Best for
Build conversion funnel analysis queries in BigQuery (Google Analytics 4 export) to identify drop-off points, optimize conversion rates, and attribute revenue to acquisition channels.
Explore other prompt categories
Move sideways into adjacent libraries when the current category is not the full answer.
Free browsing stays open. Premium prompts unlock the reusable workflow layer.
Use the guides and role paths to validate the job first. Upgrade when you want the full prompt text, editable premium prompts, and the surrounding course paths in one place.
Free access
- Browse guides, role paths, and category pages.
- Preview prompts before you decide to upgrade.
- Find the right starting point without friction.
Membership access
- Unlock premium prompts and the full copy text.
- See more workflow paths and course connections.
- Keep the reusable templates in one place.