Schema
Table
Setup
| Column Name | Type | Description |
|---|---|---|
| user_id | integer | User identifier. |
| content_id | integer | Watched content identifier. |
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Content identifier. |
| genre | varchar | Genre name. |
Find users who watched content from more than 2 distinct genres.
Schema
| Column Name | Type | Description |
|---|---|---|
| user_id | integer | User identifier. |
| content_id | integer | Watched content identifier. |
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Content identifier. |
| genre | varchar | Genre name. |
Sample Data
| user_id | content_id |
|---|---|
| 101 | 1 |
| 101 | 2 |
| 101 | 3 |
| 102 | 1 |
| 102 | 2 |
| 103 | 1 |
| 104 | 2 |
| 104 | 3 |
| content_id | genre |
|---|---|
| 1 | Drama |
| 2 | Comedy |
| 3 | Action |
| user_id |
|---|
| 101 |
| 104 |
SQL Editor
| user_id |
|---|
| 101 |
| 104 |
Hints
Solution
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
Join each watched content row to its genre.
Group the result by user_id.
Keep users with more than two distinct genres.
Related Questions