Compute Each Player's Running Total of Games Played by Date

Quick Overview

Write a SQL query that returns, for every player and date, the total number of games that player has played up to and including that date. It checks running totals with window functions or self-joins, choosing the right partition, and ordering the output.

Compute Each Player's Running Total of Games Played by Date

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `Activity` table has one row per player per day, with the device used and the number of games played that day. For each player and date, return `player_id`, `event_date`, and `games_played_so_far` (total games played by the player up to and including that date). Order by `player_id` and `event_date`. ```hint Which rows feed each total For one output row, list the input rows whose games belong in its total. Then think about how to get that set for every row without writing a separate query per date. ``` ### Input Table(s) `Activity` | Column | Type | Meaning | |---|---|---| | player_id | INT | Player | | device_id | INT | Device the player used that day | | event_date | DATE | Day of the activity | | games_played | INT | Games the player played that day | Key: `(player_id, event_date)` is the composite primary key. ### Output Contract - One row per row of `Activity`, with columns `player_id`, `event_date`, `games_played_so_far`. - `games_played_so_far` is the sum of `games_played` over all of that player's rows with `event_date` on or before the row's `event_date`. - Order by `player_id` ascending, then `event_date` ascending. ### Example `Activity` | player_id | device_id | event_date | games_played | |---|---|---|---| | 1 | 10 | 2024-01-05 | 3 | | 1 | 11 | 2024-01-02 | 2 | | 1 | 10 | 2024-01-09 | 0 | | 2 | 20 | 2024-01-03 | 4 | | 2 | 20 | 2024-01-04 | 1 | | 3 | 30 | 2024-01-06 | 7 | Expected result: | player_id | event_date | games_played_so_far | |---|---|---| | 1 | 2024-01-02 | 2 | | 1 | 2024-01-05 | 5 | | 1 | 2024-01-09 | 5 | | 2 | 2024-01-03 | 4 | | 2 | 2024-01-04 | 5 | | 3 | 2024-01-06 | 7 | Player 1's days in date order are 2024-01-02 (2 games), 2024-01-05 (3) and 2024-01-09 (0), so the running totals are 2, 5 and 5. The total carries across the switch from device 11 to device 10, and a zero-game day repeats the previous total. Player 2 reaches 4 and then 5, and player 3 has a single day with 7. ### Constraints and Clarifications - `(player_id, event_date)` is the composite primary key, so neither column is NULL and a player has at most one row per date. - `games_played >= 0` on every row. - `Activity` has at most 10,000 rows. - Rows are stored in no particular order. - `device_id` does not affect the total: games on different devices all count toward the same player.

Overview: Write a SQL query that returns, for every player and date, the total number of games that player has played up to and including that date. It checks running totals with window functions or self-joins, choosing the right partition, and ordering the output.

Amazon logo
Amazon
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The Activity table has one row per player per day, with the device used and the number of games played that day. For each player and date, return player_id, event_date, and games_played_so_far (total games played by the player up to and including that date). Order by player_id and event_date.

Input Table(s)

Activity

ColumnTypeMeaning
player_idINTPlayer
device_idINTDevice the player used that day
event_dateDATEDay of the activity
games_playedINTGames the player played that day

Key: (player_id, event_date) is the composite primary key.

Output Contract

  • One row per row of Activity , with columns player_id , event_date , games_played_so_far .
  • games_played_so_far is the sum of games_played over all of that player's rows with event_date on or before the row's event_date .
  • Order by player_id ascending, then event_date ascending.

Example

Activity

player_iddevice_idevent_dategames_played
1102024-01-053
1112024-01-022
1102024-01-090
2202024-01-034
2202024-01-041
3302024-01-067

Expected result:

player_idevent_dategames_played_so_far
12024-01-022
12024-01-055
12024-01-095
22024-01-034
22024-01-045
32024-01-067

Player 1's days in date order are 2024-01-02 (2 games), 2024-01-05 (3) and 2024-01-09 (0), so the running totals are 2, 5 and 5. The total carries across the switch from device 11 to device 10, and a zero-game day repeats the previous total. Player 2 reaches 4 and then 5, and player 3 has a single day with 7.

Constraints and Clarifications

  • (player_id, event_date) is the composite primary key, so neither column is NULL and a player has at most one row per date.
  • games_played >= 0 on every row.
  • Activity has at most 10,000 rows.
  • Rows are stored in no particular order.
  • device_id does not affect the total: games on different devices all count toward the same player.