Gamers Analysis – 1

Write an SQL query and identify first login date for each gamer.


 

Input Table: gamers_activity

player_id device_id event_date games_played
7 22 2017-04-02 4
7 22 2017-06-05 3
9 33 2018-02-26 0
6 11 2017-05-07 7
6 44 2019-08-06 3

 

Output Table:

player_id first_login
7 2017-04-02
9 2017-04-02
6 2017-04-02

Explanation:

“gamers_activity” table shows the activity of gamers who played any game. Each row represents a record of gamer who played a game on that particular date before logging out. He might not have played any game as well ( that’s why we have one record with 0 as games_played in input table). 

Identify first login_date of each gamer irrespective of whether he played any game or not on very first day.


DDLs:

CREATE TABLE IF NOT EXISTS gamers_activity (
    player_id INT,
    device_id INT,
    event_date DATE,
    games_played INT
);
INSERT INTO gamers_activity VALUES (7, 22, '2017-04-02', 4);
INSERT INTO gamers_activity VALUES (7, 22, '2017-06-05', 3);
INSERT INTO gamers_activity VALUES (9, 33, '2018-02-26', 0);
INSERT INTO gamers_activity VALUES (6, 11, '2017-05-07', 7);
INSERT INTO gamers_activity VALUES (6, 44, '2019-08-06', 3);

Solution:

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

 

Nikhil Aggarwal

Written by Nikhil Aggarwal

Nikhil Aggarwal is an ML engineer with 15+ years in data engineering, distributed systems and cloud. He builds ML and GenAI capabilities for enterprise data platforms, and writes DataForGeeks to explain complex topics through real production problems.

About me · LinkedIn · Newsletter

Leave a Comment