Skip to main content
SQL • LESSON 190

Revenue Ranking

How can we rank months by revenue?

Advanced3 Minutes2030 XP
🤔 THE QUESTION

How can we rank months by revenue?

💡 WHAT IS IT?

Encapsulating monthly revenue in a CTE and applying RANK() OVER (ORDER BY revenue DESC) ranks peak revenue months.

🎯 WHAT IS IT USED FOR?

Seasonality peak detection, highest grossing month league tables, financial performance scoring.

💻 EXAMPLE
WITH monthly_revenue AS (
    SELECT
        EXTRACT(YEAR FROM order_date) AS year,
        EXTRACT(MONTH FROM order_date) AS month,
        SUM(amount) AS revenue
    FROM orders
    GROUP BY
        EXTRACT(YEAR FROM order_date),
        EXTRACT(MONTH FROM order_date)
)
SELECT
year,
month,
revenue,
RANK() OVER (ORDER BY revenue DESC) AS revenue_rank
FROM monthly_revenue;

🎯 Mission Objectives

Practice typing production-grade SQL code for Revenue Ranking.

  • CTE + RANK
  • Revenue ranking by month
  • Peak seasonality scoring
  • Analytical pipeline