Find Cumulative Salary of an Employee - Problem

Imagine you're building a payroll analytics system for a company that needs to track employee earnings over time. Your task is to calculate a rolling 3-month salary summary for each employee.

Given an Employee table with salary records, you need to:

  • Calculate cumulative salary: For each month an employee worked, sum their salary for that month plus the previous 2 months
  • Handle missing months: If an employee didn't work in previous months, treat those salaries as 0
  • Exclude most recent month: Don't include the 3-month sum for the employee's most recent working month
  • Skip non-working months: Only include months where the employee actually worked

Table Schema:

Column NameType
idint
monthint
salaryint

Return results ordered by id ascending, then by month descending for ties.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Employee table: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 1 | 1000 | | 1 | 2 | 2000 | | 1 | 3 | 3000 | | 1 | 4 | 4000 | +----+-------+--------+
โ€บ Output: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 3 | 6000 | | 1 | 2 | 3000 | | 1 | 1 | 1000 | +----+-------+--------+
๐Ÿ’ก Note: Employee 1 worked for 4 months. We exclude month 4 (most recent). For month 3: sum(1000+2000+3000)=6000. For month 2: sum(1000+2000)=3000. For month 1: sum(1000)=1000.
example_2.sql โ€” Multiple Employees
$ Input: Employee table: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 1 | 1000 | | 1 | 2 | 2000 | | 2 | 1 | 5000 | +----+-------+--------+
โ€บ Output: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 1 | 1000 | +----+-------+--------+
๐Ÿ’ก Note: Employee 1: Most recent month is 2, so we only include month 1 with salary 1000. Employee 2: Only worked 1 month, so after excluding the most recent month (1), no records remain.
example_3.sql โ€” Gap in Employment
$ Input: Employee table: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 1 | 1000 | | 1 | 3 | 3000 | | 1 | 7 | 7000 | +----+-------+--------+
โ€บ Output: +----+-------+--------+ | id | month | salary | +----+-------+--------+ | 1 | 3 | 4000 | | 1 | 1 | 1000 | +----+-------+--------+
๐Ÿ’ก Note: Employee didn't work in months 2,4,5,6. Most recent is month 7 (excluded). Month 3 sum: 1000+3000=4000 (only actual working months). Month 1 sum: 1000 (only this month).

Visualization

Tap to expand
๐Ÿ“Š Payroll Analytics: 3-Month Rolling Summary๐Ÿ‘ค Employee RecordsEMP001: Jan($1000), Feb($2000)Mar($3000), Apr($4000)EMP002: Jan($5000)Raw salary data by month๐Ÿ”„ Rolling Window3MCurrent + Previous 2 monthsMar: $1000+$2000+$3000Feb: $1000+$2000Sliding 3-month window๐Ÿ“‹ Final ReportEMP001: Mar($6000), Feb($3000), Jan($1000)โŒ Apr excluded (most recent)EMP002: (no results)โŒ Only 1 month of dataOrdered Results:ID ASC, Month DESC๐Ÿ’ก Business Logic Rules1. Calculate 3-month rolling salary sum for performance tracking2. Exclude most recent month (incomplete data)3. Only include months with actual employment4. Handle gaps in employment naturally (missing months = $0)โšก Window Function AdvantageSUM() OVER (PARTITION BY id ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)Single pass through data vs multiple nested subqueries - O(n log n) vs O(nยฒ)
Understanding the Visualization
1
Identify Active Periods
Find all months where each employee worked and received salary
2
Calculate Rolling Windows
For each active month, sum current + previous 2 months of earnings
3
Exclude Current Period
Remove the most recent month from each employee's results
4
Generate Report
Sort results by employee ID, then by month descending
Key Takeaway
๐ŸŽฏ Key Insight: Window functions transform a complex multi-pass problem into an elegant single-pass solution, leveraging the database engine's optimized sorting and partitioning capabilities.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single scan with sorting for window function partitioning

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for intermediate results and window calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Employee.id โ‰ค 104
  • 1 โ‰ค Employee.month โ‰ค 12
  • 1 โ‰ค Employee.salary โ‰ค 106
  • All data is from year 2020
  • (id, month) combination is unique
Asked in
Amazon 15 Microsoft 12 Oracle 10 Google 8
28.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen