Game Play Analysis II
Problem
Table: Activity
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
+--------------+---------+
(player_id, event_date) is the primary key (combination of columns with unique values) of this table.
This table shows the activity of players of some games.
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device.
Problem Definition
Write a solution to report the device that is first logged in for each player.
Example
Output
Try It Yourself
-- TODO: Write your user queries here
Solution
To identify the first device each player logged in with, we can leverage SQL's window functions, specifically FIRST_VALUE(). This function allows us to retrieve the first occurrence of a value within a specified window partition. By partitioning the data by player_id and ordering it by event_date, we can effectively determine the initial device used by each player.
- Partition Data by Player: Group the records based on
player_idto analyze each player's activity individually. - Order Events Chronologically: Within each player group, order the records by
event_dateto identify the sequence of device usage. - Retrieve the First Device: Use the
FIRST_VALUE()window function to extract the device ID from the earliest event for each player. - Eliminate Duplicate Entries: Apply
DISTINCTto ensure each player appears only once in the final result.
SQL Query
SELECT DISTINCT player_id, FIRST_VALUE(device_id) OVER ( PARTITION BY player_id ORDER BY event_date ) AS device_id FROM Activity;
Step-by-Step Approach
Step 1: Partition Data by Player and Order by Event Date
Group the activity records by player_id and order each group chronologically based on event_date to prepare for identifying the first device used.
SQL Query:
SELECT player_id, device_id, event_date, FIRST_VALUE(device_id) OVER ( PARTITION BY player_id ORDER BY event_date ) AS first_device FROM Activity;
Explanation:
SELECT player_id, device_id, event_date, FIRST_VALUE(device_id) OVER (...) AS first_device:- Retrieves each player's ID, the device used, the date of the event, and the first device used by the player.
FIRST_VALUE(device_id) OVER (PARTITION BY player_id ORDER BY event_date) AS first_device:- The
FIRST_VALUE()function scans each partition (grouped byplayer_id) and retrieves thedevice_idfrom the earliestevent_date.
- The
FROM Activity:- Indicates that the data is sourced from the
Activitytable.
- Indicates that the data is sourced from the
Output After Step 1:
Based on the example input, the intermediate result would be:
+-----------+-----------+-------------+-------------+ | player_id | device_id | event_date | first_device| +-----------+-----------+-------------+-------------+ | 1 | 2 | 2016-03-01 | 2 | | 1 | 2 | 2016-05-02 | 2 | | 2 | 3 | 2016-03-01 | 3 | | 3 | 1 | 2016-03-01 | 1 | | 3 | 4 | 2018-07-03 | 1 | +-----------+-----------+-------------+-------------+
Step 2: Select Distinct Player IDs with Their First Device
Extract each player's ID along with their first device, ensuring that each player appears only once in the final result.
SQL Query:
SELECT DISTINCT player_id, FIRST_VALUE(device_id) OVER ( PARTITION BY player_id ORDER BY event_date ) AS device_id FROM Activity;
Explanation:
SELECT DISTINCT player_id, FIRST_VALUE(device_id) OVER (...) AS device_id:- Selects each unique
player_idand associates it with the firstdevice_iddetermined by the window function.
- Selects each unique
Final Output:
+-----------+-----------+ | player_id | device_id | +-----------+-----------+ | 1 | 2 | | 2 | 3 | | 3 | 1 | +-----------+-----------+
🤖 Don't fully get this? Learn it with Claude
Stuck on Game Play Analysis II? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Game Play Analysis II** (Databases) and want to truly understand it. Explain Game Play Analysis II from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Game Play Analysis II** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Game Play Analysis II** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Game Play Analysis II** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.