How do you optimize SQL queries for performance in a warehouse environment?
Treat this as a hands-on SQL exercise. Using the provided warehouse tables, write an efficient query that preserves correctness while calculating monthly revenue metrics and comparisons.
Output
- One row per month, region, and product category.
- Columns:
month_start, region, category, revenue, order_count, prior_month_revenue, revenue_change_pct, and monthly_revenue_rank.
- Include completed sales from January through March 2025, order by
month_start, revenue descending, region, and category.
Output details
- Use sales dates from
2025-01-01 inclusively through, but excluding, 2025-04-01.
- Count every qualifying sales row in
order_count, without deduplication.
- Calculate
prior_month_revenue within each region and category using the immediately preceding available month.
- Return
NULL for missing prior revenue and when prior_month_revenue equals zero; round revenue_change_pct to two decimal places.
- Rank rows within each month by descending revenue, sharing ranks for ties and leaving gaps after tied ranks.