The Find Cumulative Salary of an Employee coding problem is a SQL task. You are given an Employee table with id, month, and salary. For each employee, you need to calculate the cumulative salary for the last 3 months (current month + 2 previous months), excluding the most recent month they worked. The output should be grouped by employee ID and month, sorted descending by both.
Amazon and other companies use this "Hard" SQL problem to evaluate your proficiency with Window Functions. It tests your ability to handle complex temporal constraints (the 3-month window) and conditional filtering (excluding the most recent month). It’s a realistic data reporting task used in payroll and performance analysis.
This problem uses SQL Window Functions (SUM() OVER) and Subqueries.
MAX(month) OVER(PARTITION BY id).month == max_month.SUM(salary) OVER(PARTITION BY id ORDER BY month RANGE BETWEEN 2 PRECEDING AND CURRENT ROW).
ROWS or manual joins if RANGE is not supported for specific windows.Employee 1 Data: (Jan, 20), (Feb, 30), (Mar, 40), (Apr, 50).
(Mar, 90), (Feb, 50), (Jan, 20).ROWS BETWEEN instead of RANGE BETWEEN when months might be missing (though the problem usually implies contiguous months).Practice using OVER (PARTITION BY ... ORDER BY ...) clauses. They are the most powerful tool in SQL for calculating running totals, averages, and rankings within specific groups.