CMD Guide
HomeDatabasesSQL Practice Problems

Game Play Analysis I

GROUP BY player_id tells the engine to shred the Activity rows into one bucket per distinct player_id, and MIN(event_date) then collapses each bucket to its single smallest date — so the whole query is "partition the rows, then reduce each partition to one value."

The task

Table Activity has primary key (player_id, event_date) — one row per player per day they logged in. We want each player's first login date (the earliest event_date).

SELECT player_id,
       MIN(event_date) AS first_login
FROM Activity
GROUP BY player_id;

This is the entire, correct solution. The interesting part is how the engine produces it, and the ways the obvious variants silently break.

Worked trace on the real sample

The sample Activity table:

player_iddevice_idevent_dategames_played
122016-03-015
122016-05-026
232017-06-251
312016-03-020
342018-07-035

Step 1 — partition by player_id. The engine does not first produce a flat list of (player_id, event_date) rows; the GROUP BY is logically applied before the SELECT expressions are evaluated. After grouping there are three buckets:

Step 2 — reduce each bucket with MIN. MIN compares dates as ordered values (lexicographic on ISO YYYY-MM-DD happens to match chronological order):

bucketdates seenMIN(event_date)
player 12016-03-01, 2016-05-022016-03-01
player 22017-06-252017-06-25
player 32016-03-02, 2018-07-032016-03-02

Result — one row per bucket: (1, 2016-03-01), (2, 2017-06-25), (3, 2016-03-02).

diagram
diagram

Why the naive intermediate is wrong

A common (and previously-on-this-page) mistake is to show an "after the SELECT, before the GROUP BY" intermediate like:

-- INVALID as a standalone query
SELECT player_id, MIN(event_date)
FROM Activity;        -- no GROUP BY

and then claim it yields five rows (1 | 2016-03-01, 1 | 2016-03-01, 2 | …, 3 | …, 3 | …). That table is fabricated — it cannot occur:

The lesson: there is no "per-row" stage that pairs each player_id with a min. Aggregation operates on groups, and the group is decided by GROUP BY before MIN ever runs. The correct query with GROUP BY player_id is what makes player_id a legal output column.

MIN vs. the window-function alternative

You can also get "earliest per player" with a window function, and it is worth knowing when each wins:

-- Window version: ranks rows within each player
SELECT player_id, event_date AS first_login
FROM (
  SELECT player_id, event_date,
         ROW_NUMBER() OVER (
           PARTITION BY player_id
           ORDER BY event_date
         ) AS rn
  FROM Activity
) t
WHERE rn = 1;
GROUP BY MINROW_NUMBER window
Outputone aggregated value per playerthe whole row of the earliest login
Need other cols from that row?No — MIN gives only the dateYes — keeps device_id, games_played of that row
Costcheap; no sort if an index supplies orderrequires a partitioned sort / extra pass

For this problem (only the date is asked for) GROUP BY MIN is the right tool — simpler and cheaper. Reach for the window version when you need the device or games-played of the first login (that is Game Play Analysis II/IV territory).

Pitfalls

Takeaways


Sources: LeetCode 511 "Game Play Analysis I"; MySQL 8.0 Reference Manual — "Detection of Functional Dependence" and the ONLY_FULL_GROUP_BY SQL mode; the SQL:2016 standard rules for grouped queries; MySQL's GROUP BY loose index scan optimization. Re-authored and deepened for this guide — the fabricated five-row "Output After Step 1" intermediate was removed and replaced with the correct partition-then-reduce mechanism, a MIN-vs-window comparison, tie/NULL/index pitfalls, and a hand-authored diagram.

🤖 Don't fully get this? Learn it with Claude

Stuck on Game Play Analysis I? 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 I** (Databases) and want to truly understand it. Explain Game Play Analysis I 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 I** 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 I** 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 I** 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