cswork / libraryOpen practice workspace

Coding questions · Tencent

Compute per-player daily cumulative games with windows

This question evaluates proficiency with SQL window functions, aggregation, date-aware grouping and joins for computing per-player daily counts and running totals, and it falls under Data Manipulation (SQL/Python) for Data Scientist roles, emphasizing practical application rather than purely theoretical concepts.

Write a SQL query to output, for each player_id and each calendar date present in the data, the cumulative number of games played up to and including that date. Use window functions. Avoid user‑defined functions. Schema and sample data:

Tables:

  • game_sessions(session_id INT PRIMARY KEY, player_id INT, game_id INT, session_date DATE)

Sample rows (game_sessions):

+------------+-----------+---------+--------------+
| session_id | player_id | game_id | session_date |
+------------+-----------+---------+--------------+
| 1 | 101 | 1 | 2025-08-29 |
| 2 | 101 | 2 | 2025-08-29 |
| 3 | 101 | 1 | 2025-08-30 |
| 4 | 102 | 2 | 2025-08-30 |
| 5 | 101 | 3 | 2025-09-01 |
| 6 | 102 | 2 | 2025-09-01 |
+------------+-----------+---------+--------------+

Part A: Treat each row as one game played. Output columns: player_id, session_date, daily_games (count of sessions that day for that player), cum_games (cumulative count per player ordered by date). Use only window functions for the cumulative part.

Part B (harder): If you must include zero‑activity dates between a player’s first and last session, show how you would modify the query assuming a calendar(d DATE) table that covers 2025-08-29 through 2025-09-01. Do not use recursive CTEs.

Daily and cumulative games per player (non-zero days only)

You are given a game_sessions table that records individual game sessions. Each row represents one game played by a player on a specific date.

Write a SQL query to output, for each player_id and each session_date on which that player has at least one session, the cumulative number of games played up to and including that date.

Treat each row as one game played.

Output columns:

  • player_id
  • session_date
  • daily_games: count of sessions that day for that player
  • cum_games: cumulative count of games per player, ordered by session_date

Use window functions for the cumulative part (cum_games). Do not use user-defined functions.

Table game_sessions

columntypeconstraints
session_idINTPRIMARY KEY, NOT NULL
player_idINTNOT NULL
game_idINTNOT NULL
session_dateDATENOT NULL

Sample data:

game_idplayer_idsession_datesession_id
11012025-08-291
21012025-08-292
11012025-08-303
21022025-08-304
31012025-09-015
21022025-09-016

Expected output

One row per player_id and session_date on which they played at least one game, with daily and cumulative counts per player ordered by date.

cum_gamesdaily_gamesplayer_idsession_date
221012025-08-29
311012025-08-30
411012025-09-01
111022025-08-30
211022025-09-01

Hints

  1. First aggregate game_sessions by player_id and session_date to get daily_games.
  2. Use SUM(daily_games) as a window function partitioned by player_id and ordered by session_date for the cumulative total.

Daily and cumulative games per player including zero-activity dates

Extend the previous task so that zero-activity dates between a player’s first and last session are included.

You are given two tables:

  • game_sessions: records individual game sessions (each row is one game played).
  • calendar: a date dimension table that contains one row per calendar date and covers the full range from 2025-08-29 through 2025-09-01.

For each player_id, consider the period from their first session_date to their last session_date (inclusive). For every date in this range, output a row, even if the player did not play any games on that date (in which case daily_games should be 0).

Output columns:

  • player_id
  • session_date
  • daily_games: count of sessions that day for that player (0 if none)
  • cum_games: cumulative count of games per player, ordered by session_date

Use window functions for the cumulative part (cum_games). Do not use user-defined functions. Do not use recursive CTEs. Use the calendar table to generate the missing dates.

Table game_sessions

columntypeconstraints
session_idINTPRIMARY KEY, NOT NULL
player_idINTNOT NULL
game_idINTNOT NULL
session_dateDATENOT NULL

Sample data:

game_idplayer_idsession_datesession_id
11012025-08-291
21012025-08-292
11012025-08-303
21022025-08-304
31012025-09-015
21022025-09-016

Table calendar

columntypeconstraints
dDATEPRIMARY KEY, NOT NULL

Sample data:

d
2025-08-29
2025-08-30
2025-08-31
2025-09-01

Expected output

One row per player_id and per calendar date between that player’s first and last session (inclusive), including dates with zero games, with daily and cumulative counts per player ordered by date.

cum_gamesdaily_gamesplayer_idsession_date
221012025-08-29
311012025-08-30
301012025-08-31
411012025-09-01
111022025-08-30
101022025-08-31
211022025-09-01

Hints

  1. Compute each player’s first and last session_date, then join their date range to the calendar table.
  2. Left join the per-player-per-day counts to the generated player/date grid, COALESCE missing counts to 0, then apply a window SUM for the cumulative total.