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_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 |
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:
- Player 1 → { 2016-03-01, 2016-05-02 }
- Player 2 → { 2017-06-25 }
- Player 3 → { 2016-03-02, 2018-07-03 }
Step 2 — reduce each bucket with MIN. MIN compares dates as ordered values (lexicographic on ISO YYYY-MM-DD happens to match chronological order):
| bucket | dates seen | MIN(event_date) |
|---|---|---|
| player 1 | 2016-03-01, 2016-05-02 | 2016-03-01 |
| player 2 | 2017-06-25 | 2017-06-25 |
| player 3 | 2016-03-02, 2018-07-03 | 2016-03-02 |
Result — one row per bucket: (1, 2016-03-01), (2, 2017-06-25), (3, 2016-03-02).
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 BYand then claim it yields five rows (1 | 2016-03-01, 1 | 2016-03-01, 2 | …, 3 | …, 3 | …). That table is fabricated — it cannot occur:
- With
ONLY_FULL_GROUP_BYenabled (MySQL 5.7+ default, and the SQL standard), this query is rejected: "player_id is not in GROUP BY clause and contains a nonaggregated column". PostgreSQL, SQL Server, and Oracle reject it outright. - In MySQL's old loose mode it does not error — but the moment any aggregate (
MIN) appears with noGROUP BY, the whole table becomes one single group. So it returns exactly one row: an arbitraryplayer_idpaired with the global minimum date (2016-03-01). Never five rows.
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 MIN | ROW_NUMBER window | |
|---|---|---|
| Output | one aggregated value per player | the whole row of the earliest login |
| Need other cols from that row? | No — MIN gives only the date | Yes — keeps device_id, games_played of that row |
| Cost | cheap; no sort if an index supplies order | requires 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
- Selecting a bare column that isn't grouped.
SELECT player_id, device_id, MIN(event_date) … GROUP BY player_id—device_idis neither grouped nor aggregated. Standard SQL andONLY_FULL_GROUP_BYreject it; loose MySQL silently returns an arbitrarydevice_idthat may not belong to the first-login row. To return that device, use the window version. - Assuming MIN breaks ties uniquely. The primary key is
(player_id, event_date), so a player cannot have two rows on the same date — first login is unambiguous here. Remove that PK guarantee (e.g., timestamps with duplicates) andMINstill returns one date, butROW_NUMBER()would arbitrarily pick one of the tied rows unless you add a deterministic tiebreaker inORDER BY. - NULL dates.
MINignores NULLs. If a player had only NULLevent_daterows,MINreturns NULL for that group rather than dropping the player — and the player still appears in the result. - Index angle. A composite index on
(player_id, event_date)— which the primary key already provides in most engines — lets the optimizer satisfyMIN(event_date)per group by a loose index scan: it jumps to the firstevent_datefor eachplayer_idprefix instead of scanning every row. Without that ordering the engine must scan and hash/sort all rows. - String dates. If
event_datewere stored as text in a non-ISO format (e.g.MM/DD/YYYY),MINcompares lexicographically and returns the wrong "earliest." ISOYYYY-MM-DDor a realDATEtype is what makesMINcorrect.
Takeaways
GROUP BYruns logically beforeSELECT: it forms buckets, then each aggregate reduces one bucket to one value — there is no per-row intermediate to inspect.- An aggregate with a bare non-grouped column is invalid under
ONLY_FULL_GROUP_BY; with noGROUP BYat all it produces exactly one row, never one-per-key. - Use
GROUP BY MINwhen you need the value; use aROW_NUMBER()window when you need the whole earliest row. - A
(player_id, event_date)index turns this into a loose index scan — fast and order-aware — so the right index is also the right correctness guarantee for tie-free first logins.
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.
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.
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.
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.
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.