Knowledge Guide
HomeDatabasesSQL Practice Problems

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

Image
Image

Output

Image
Image

Try It Yourself

java
-- 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.

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:

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:

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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes