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 player
  • device_id - device used for the session
  • event_date - date of the gaming session
  • games_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_date is a valid date
  • 0 ≤ games_played ≤ 100

Visualization

Tap to expand
Game Play Analysis II: Find First Login DeviceInput: Activityplayer_iddevice_idevent_dategames122016-03-015132016-03-026242017-06-251Find FirstPer PlayerOutput: First Devicesplayer_iddevice_id1224Window function identifies earliest login date for each playerand returns the device_id from that first session
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
Asked in
Amazon 25 Facebook 18
28.5K Views
Medium Frequency
~12 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