3898. Seasonal Sales Analysis
My accepted SQL solution to LeetCode problem 3898, Seasonal Sales Analysis, running in 490ms.
- Difficulty: Medium
- SQL
- Runtime 490ms
- Memory 0.0B
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
SQL
Accepted on LeetCode — runtime 490ms, memory 0.0B, accepted 2026-01-02.
SELECT season, category, total_quantity, total_revenue
FROM (
SELECT
season,
category,
total_quantity,
total_revenue,
ROW_NUMBER() OVER (
PARTITION BY season
ORDER BY total_quantity DESC, total_revenue DESC
) AS rn
FROM (
SELECT
CASE
WHEN MONTH(s.sale_date) IN (12, 1, 2) THEN 'Winter'
WHEN MONTH(s.sale_date) IN (3, 4, 5) THEN 'Spring'
WHEN MONTH(s.sale_date) IN (6, 7, 8) THEN 'Summer'
ELSE 'Fall'
END AS season,
p.category,
SUM(s.quantity) AS total_quantity,
SUM(s.quantity * s.price) AS total_revenue
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY 1, 2
) seasonal_sales
) ranked
WHERE rn = 1
ORDER BY season