Skip to content

Commit e908064

Browse files
Update SQL_Medium_Complex_Interview_Problems.md
1 parent 9284678 commit e908064

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

SQL_Medium_Complex_Interview_Problems.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,84 @@ ORDER BY
500500

501501
```
502502

503+
# Game Analysis I-1V
504+
505+
```sql
506+
507+
create table activity (
508+
509+
player_id int ,
510+
device_id int ,
511+
event_date date ,
512+
games_played int
513+
);
514+
515+
insert into activity values (1,2,'2016-03-01',5 ),(1,2,'2016-03-02',6 ),(2,3,'2017-06-25',1 )
516+
,(3,1,'2016-03-02',0 ),(3,4,'2018-07-03',5 );
517+
518+
select * from activity;
519+
520+
```
521+
522+
523+
# Questions:
524+
525+
```sql
526+
527+
Game Play Analysis
528+
529+
q1: Write an SQL query that reports the first login date for each player
530+
531+
q2: Write a SQL query that reports the device that is first logged in for each player
532+
533+
q3: Write an SQL query that reports for each player and date, how many games played so far by the player.
534+
That is, the total number of games played by the player until that date.
535+
536+
q4: Write an SQL query that reports the fraction of players that logged in again
537+
on the day after the day they first logged in, rounded to 2 decimal places
538+
539+
```
540+
541+
542+
* Solution 1 -
543+
544+
```sql
545+
546+
select player_id , min(event_date) as f_login from activity group by player_id;
547+
548+
```
549+
550+
* Solution 2 -
551+
552+
```sql
553+
554+
select player_id , min(device_id) as f_device from activity group by player_id ;
555+
556+
```
557+
558+
* Solution 3
559+
560+
```sql
561+
562+
select player_id , sum(games_played) over(partition by player_id order by event_date) as total_till_date , event_date from activity ;
563+
564+
```
565+
566+
567+
* Solution 4
568+
569+
```sql
570+
571+
572+
WITH PlayerFirstEvent AS (
573+
SELECT player_id, MIN(event_date) AS event_date
574+
FROM Activity
575+
GROUP BY player_id
576+
)
577+
578+
SELECT ROUND(COUNT(DISTINCT b.player_id) / COUNT(DISTINCT a.player_id), 2) AS fraction
579+
FROM PlayerFirstEvent a
580+
LEFT JOIN Activity b ON a.player_id = b.player_id AND DATEDIFF(b.event_date, a.event_date) = 1;
581+
582+
```
583+

0 commit comments

Comments
 (0)