Game Play Analysis II - Problem
Given a table Activity that tracks player game sessions, write a SQL query to find the first device each player used when they logged in.
The Activity table contains:
player_id- unique identifier for each playerdevice_id- device used for the sessionevent_date- date of the gaming sessiongames_played- number of games played in that session
Return the result showing each player and their first login device in any order.
Table Schema
Activity
| Column Name | Type | Description |
|---|---|---|
player_id
PK
|
int | Unique identifier for each player |
device_id
|
int | Device identifier used for the session |
event_date
PK
|
date | Date when the player logged in |
games_played
|
int | Number of games played in the session |
Primary Key: (player_id, event_date)
Note: Each row represents a gaming session for a player on a specific date
Input & Output
Example 1 — Multiple Players with Multiple Sessions
Input Table:
| player_id | device_id | event_date | games_played |
|---|---|---|---|
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 3 | 2016-03-02 | 6 |
| 2 | 4 | 2017-06-25 | 1 |
Output:
| player_id | device_id |
|---|---|
| 1 | 2 |
| 2 | 4 |
💡 Note:
Player 1 first logged in on 2016-03-01 using device 2, then again on 2016-03-02 using device 3. Player 2 only has one session on 2017-06-25 using device 4. The output shows each player's first login device.
Example 2 — Single Player Multiple Devices
Input Table:
| player_id | device_id | event_date | games_played |
|---|---|---|---|
| 3 | 1 | 2018-07-03 | 0 |
| 3 | 4 | 2018-07-01 | 5 |
Output:
| player_id | device_id |
|---|---|
| 3 | 4 |
💡 Note:
Player 3 has two sessions: one on 2018-07-01 with device 4 and another on 2018-07-03 with device 1. Since the session on 2018-07-01 is earlier, device 4 is the first device used by player 3.
Constraints
-
1 ≤ player_id ≤ 1000 -
1 ≤ device_id ≤ 1000 -
event_dateis a valid date -
0 ≤ games_played ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Activity table with player sessions
2
Window Function
Rank sessions by date per player
3
Output
Player and their first device
Key Takeaway
🎯 Key Insight: Use window functions to find the first occurrence within each group efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code