0Pricing
SQL Academy · Lesson

Real-World Reporting Patterns

Implement classic dashboards: retention curves, top-N per category, sessionisation — all with window functions.

Pattern: Top-N Per Group

Top 3 orders per user:

WITH ranked AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY total DESC) AS rn
  FROM orders
)
SELECT * FROM ranked WHERE rn <= 3;

Pattern: Running Totals

Cumulative revenue over time:

SELECT day, revenue,
       SUM(revenue) OVER (ORDER BY day) AS running_total
FROM daily_revenue;

All lessons in this course

  1. Frame Clauses: ROWS vs RANGE
  2. Lag/Lead with Frame Windows
  3. Bucketing with NTILE and Cume_Dist
  4. Real-World Reporting Patterns
← Back to SQL Academy