Skip to main content

Command Palette

Search for a command to run...

LeetCode 511: Game Play Analysis I

LeetCode 511: Solution for Game Play Analysis I

Updated
2 min read
LeetCode 511: Game Play Analysis I
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: January 22, 2026
Category: SQL
Time Taken: 5 minutes
Difficulty: Easy


Problem Statement

Write a solution to find the first login date for each player.

Link: Game Play Analysis I


My Approach

Initial thought:

We can use GROUP BY for each player and apply the MIN operator on the event_date column to find the earliest date for each player. This will identify the first login for every player, as MIN will select the earliest dates.

Final solution:

  • A better approach is to use Window functions, specifically ROW_NUMBER().

  • Using ROW_NUMBER() with PARTITION BY player_id and ORDER BY event_date ensures that each date for every player gets a unique rank, allowing us to easily find the first rank for the solution.

Why this works:
This solution is more efficient, easy to read, retains row-level details for future use, and requires scanning the table only once, unlike the GROUP BY solution, which scans the table twice.


Solution Code (MIN-GROUP BY) [Faster on small datasets]

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

Solution Code (Windows Function) [BETTER]

WITH CTE AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY player_id ORDER BY event_date) AS rnk
FROM Activity)

SELECT player_id, event_date AS first_login
FROM CTE
WHERE rnk = 1

Key Takeaway: Use ROW_NUMBER() to pick exactly one unique record per group (deduplication), and use DENSE_RANK() to include all tied records for a specific position (ranking).

Pattern: Window Function

Mistakes I Made: Used DENSE_RANK instead of ROW_NUMBER initially.

Series: 90 Days of Data Engineering Progress: 15/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 16 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 20: Valid Parentheses

Solution for LeetCode 20: Valid Parentheses