Game Play Analysis I - Problem
You are analyzing player activity data for a popular gaming platform. The platform tracks when players log in and play games throughout different days.
Your task: Find the first login date for each player in the database.
You are given an Activity table with the following structure:
| Column Name | Type |
|---|---|
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
Key Information:
- The combination of
(player_id, event_date)is unique (primary key) - Each row represents a player's activity on a specific date
- A player might have logged in multiple times on different dates
games_playedcan be 0 (player logged in but didn't play any games)
Goal: Return a table with player_id and their first_login_date. The order of results doesn't matter.
Input & Output
basic_example.sql โ Basic Case
$
Input:
Activity table:
| player_id | device_id | event_date | games_played |
|-----------|-----------|------------|--------------|
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 2 | 2016-05-02 | 6 |
| 2 | 3 | 2017-06-25 | 1 |
| 3 | 1 | 2016-03-02 | 0 |
| 3 | 4 | 2018-07-03 | 5 |
โบ
Output:
| player_id | first_login_date |
|-----------|-----------------|
| 1 | 2016-03-01 |
| 2 | 2017-06-25 |
| 3 | 2016-03-02 |
๐ก Note:
Player 1 first logged in on 2016-03-01, player 2 has only one record so that's their first login, and player 3 first logged in on 2016-03-02 (earlier than their 2018 login).
single_login.sql โ Single Login Per Player
$
Input:
Activity table:
| player_id | device_id | event_date | games_played |
|-----------|-----------|------------|--------------|
| 1 | 1 | 2020-01-15 | 3 |
| 2 | 2 | 2020-02-20 | 0 |
| 3 | 3 | 2020-03-25 | 7 |
โบ
Output:
| player_id | first_login_date |
|-----------|-----------------|
| 1 | 2020-01-15 |
| 2 | 2020-02-20 |
| 3 | 2020-03-25 |
๐ก Note:
Each player has only one login record, so their first login date is simply their only login date. Note that games_played can be 0.
multiple_logins.sql โ Multiple Logins Edge Case
$
Input:
Activity table:
| player_id | device_id | event_date | games_played |
|-----------|-----------|------------|--------------|
| 1 | 1 | 2019-12-31 | 1 |
| 1 | 2 | 2020-01-01 | 2 |
| 1 | 1 | 2019-11-15 | 0 |
| 2 | 3 | 2020-06-01 | 5 |
| 2 | 3 | 2020-05-31 | 3 |
โบ
Output:
| player_id | first_login_date |
|-----------|-----------------|
| 1 | 2019-11-15 |
| 2 | 2020-05-31 |
๐ก Note:
Player 1 has three logins, with the earliest being 2019-11-15. Player 2 has two logins with 2020-05-31 being earlier than 2020-06-01. The MIN function correctly identifies the earliest date for each player.
Visualization
Tap to expand
Understanding the Visualization
1
Organize by Member
Group all logbook entries by member ID, creating separate piles for each person
2
Find Earliest Date
In each member's pile, look through their visit dates and circle the earliest one
3
Create Anniversary List
Write down each member's ID and their first visit date for the celebration
Key Takeaway
๐ฏ Key Insight: SQL's GROUP BY with MIN() function is like having a super-efficient club secretary who automatically organizes member records and finds the earliest date for each person instantly.
Time & Space Complexity
Time Complexity
O(n log n)
Database engine typically sorts data for GROUP BY operations, though some engines use hash-based grouping
โก Linearithmic
Space Complexity
O(k)
Where k is the number of unique players (for storing intermediate groups)
โ Linear Space
Constraints
- 1 โค Activity table rows โค 104
- 1 โค player_id โค 104
- 1 โค device_id โค 104
- 0 โค games_played โค 1000
- event_date is in format YYYY-MM-DD
- The combination (player_id, event_date) is unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code