Digits
n Data
Netflix SQL Interview Question 09

Multi-Genre
Users

Find users who watched content from more than 2 distinct genres.

Schema

Table
Setup

Column NameTypeDescription
user_idintegerUser identifier.
content_idintegerWatched content identifier.
Column NameTypeDescription
content_idintegerContent identifier.
genrevarcharGenre name.

Sample Data

Input
Output

Sample Input: watching_activity
user_idcontent_id
1011
1012
1013
1021
1022
1031
1042
1043
Sample Input: content
content_idgenre
1Drama
2Comedy
3Action
Expected Output
user_id
101
104

SQL Editor

Run
Query

postgresql
Waiting for query

user_id
101
104

Hints

Unlock
Clues

Hint 01: The genre is in content.
Hint 02: Join on content_id.
Hint 03: Use HAVING COUNT(DISTINCT genre) > 2.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT
  w.user_id
FROM watching_activity w
JOIN content c
  ON w.content_id = c.content_id
GROUP BY w.user_id
HAVING COUNT(DISTINCT c.genre) > 2;

Explanation

Step By
Step

01

Join each watched content row to its genre.

02

Group the result by user_id.

03

Keep users with more than two distinct genres.

Related Questions

Keep
Solving