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 NameType
player_idint
device_idint
event_datedate
games_playedint

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_played can 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
๐ŸŽฎ Gaming Club Activity Logbook ๐ŸŽฎFinding each member's first visit for anniversary celebration๐Ÿ“š Member Alex (ID: 1)Visit 1: 2016-03-01 โญVisit 2: 2016-05-02Visit 3: 2016-07-15๐Ÿ† First Visit: 2016-03-01๐Ÿ“š Member Sam (ID: 2)Visit 1: 2017-06-25 โญ(Only one visit)๐Ÿ† First Visit: 2017-06-25๐Ÿ“š Member Jordan (ID: 3)Visit 1: 2016-03-02 โญVisit 2: 2018-07-03๐Ÿ† First Visit: 2016-03-02๐ŸŽ‰ Anniversary Celebration List ๐ŸŽ‰Member ID | First Visit DateAlex (1) | 2016-03-01Sam (2) | 2017-06-25Jordan (3) | 2016-03-02
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

n
2n
โšก Linearithmic
Space Complexity
O(k)

Where k is the number of unique players (for storing intermediate groups)

n
2n
โœ“ 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
Asked in
Facebook 45 Amazon 32 Microsoft 28 Google 25
68.4K Views
High Frequency
~8 min Avg. Time
2.8K 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