Game Play Analysis IV - Problem

Given a table Activity that tracks player login activity and game sessions, write a SQL query to find the fraction of players that logged in again on the day after their first login.

The table has the following structure:

  • player_id: Unique identifier for each player
  • device_id: Device used for the session
  • event_date: Date of the login session
  • games_played: Number of games played in that session

Your query should return the percentage of players who logged in on consecutive days starting from their first login, rounded to 2 decimal places.

Table Schema

Activity
Column Name Type Description
player_id PK int Unique identifier for each player
device_id int Device used for the gaming session
event_date PK date Date when the player logged in
games_played int Number of games played in that session
Primary Key: (player_id, event_date)
Note: Each row represents a unique player login session on a specific date

Input & Output

Example 1 — Mixed Retention Pattern
Input Table:
player_id device_id event_date games_played
1 2 2016-03-01 5
1 2 2016-03-02 6
2 3 2017-06-25 1
3 1 2016-03-01 0
3 4 2018-07-03 5
Output:
fraction
0.33
💡 Note:

Player 1's first login was 2016-03-01 and returned on 2016-03-02 (next day) ✓. Player 2's first login was 2017-06-25 but has no record for 2017-06-26 ✗. Player 3's first login was 2016-03-01 but next login was in 2018 ✗. Result: 1 out of 3 players = 0.33

Example 2 — No Retention
Input Table:
player_id device_id event_date games_played
1 2 2016-03-01 0
1 2 2016-03-04 1
2 3 2017-06-25 2
Output:
fraction
0
💡 Note:

Player 1's first login was 2016-03-01 but next login was 2016-03-04 (not consecutive) ✗. Player 2 only has one login record ✗. No players returned the day after their first login: 0 out of 2 players = 0.00

Constraints

  • 1 ≤ player_id ≤ 1000
  • 1 ≤ device_id ≤ 1000
  • event_date is in format 'YYYY-MM-DD'
  • games_played ≥ 0

Visualization

Tap to expand
Game Play Analysis IV - SQL Solution INPUT: Activity Table player device date games 1 2 2016-03-01 5 1 2 2016-03-02 6 2 3 2017-06-25 1 3 1 2016-03-02 0 3 4 2018-07-03 5 Player Timelines P1: D1 D2 OK P2: D1 No D2 P3: D1 No D2 Consecutive login No next-day login ALGORITHM STEPS 1 Find First Login MIN(event_date) per player SELECT player_id, MIN(event_date) as first 2 Add 1 Day DATE_ADD(first, 1 DAY) first_date + INTERVAL 1 DAY 3 Check Next-Day Login LEFT JOIN Activity WHERE a.event_date = first + 1 day 4 Calculate Fraction COUNT matches / total ROUND(COUNT(match) / COUNT(*), 2) FINAL RESULT Retention Calculation Players with consecutive login: 1 (Player 1 only) Total unique players: 3 (Players 1, 2, 3) 1 / 3 = 0.333... fraction 0.33 33% Day-1 Retention Rate Key Insight: Use a CTE or subquery to find each player's first login date, then LEFT JOIN back to the Activity table to check if they logged in the next day. The fraction is calculated by counting successful matches divided by total players. ROUND to 2 decimal places for the final result. TutorialsPoint - Game Play Analysis IV | Optimal Solution
Asked in
Facebook 12 Amazon 8 Google 6
28.5K Views
Medium Frequency
~18 min Avg. Time
892 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