Generate optimized SQL queries for common business analytics use cases.
## SQL Query Generator
Generate SQL queries for [ANALYSIS_TYPE]:
**Database:** [DATABASE — PostgreSQL/MySQL/BigQuery/Snowflake/SQL Server]
**Schema:** [SCHEMA_DESCRIPTION — describe tables and key columns]
**Analysis Type:** [ANALYSIS_TYPE]
**Time Period:** [PERIOD]
**Query Categories:**
### Revenue Analytics
```sql
-- Monthly recurring revenue (MRR) trend
SELECT
DATE_TRUNC('month', [DATE_COLUMN]) as month,
SUM([REVENUE_COLUMN]) as mrr,
LAG(SUM([REVENUE_COLUMN])) OVER (ORDER BY DATE_TRUNC('month', [DATE_COLUMN])) as prev_mrr,
ROUND((SUM([REVENUE_COLUMN]) - LAG(SUM([REVENUE_COLUMN])) OVER (ORDER BY DATE_TRUNC('month', [DATE_COLUMN])))
/ NULLIF(LAG(SUM([REVENUE_COLUMN])) OVER (ORDER BY DATE_TRUNC('month', [DATE_COLUMN])), 0) * 100, 2) as growth_pct
FROM [TABLE]
WHERE [DATE_COLUMN] >= [START_DATE]
GROUP BY 1
ORDER BY 1;
```
### Customer Analytics
```sql
-- Customer cohort retention analysis
-- Customer lifetime value calculation
-- Churn rate by segment
-- RFM segmentation
```
### Product Analytics
```sql
-- Feature adoption rates
-- Funnel conversion analysis
-- Power user identification
-- Usage frequency distribution
```
### Operational Analytics
```sql
-- Support ticket volume trends
-- SLA compliance rates
-- Employee productivity metrics
-- Inventory turnover
```
**Query Optimization Tips:**
- Use CTEs for readability over subqueries
- Add appropriate indexes: [INDEX_SUGGESTIONS]
- Partition large tables by [PARTITION_KEY]
- Use window functions for running calculations
- Avoid SELECT * in production queries
**Output:** Commented, optimized SQL ready to run.
**Variables:** Replace [DATABASE], [SCHEMA_DESCRIPTION], [ANALYSIS_TYPE], [PERIOD].Free to copy and use. Compatible with Claude 4 Opus, Gemini 2.5 Pro.
Replace all [BRACKETED] variables with your specific data and requirements.
Initial release
Sign in and download this prompt to leave a review.