Schema
Table
Setup
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Content identifier. |
| genre | varchar | Genre name. |
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Watched content identifier. |
| hours_watched | decimal | Watch duration. |
Find the most popular genre based on total watch time.
Schema
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Content identifier. |
| genre | varchar | Genre name. |
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Watched content identifier. |
| hours_watched | decimal | Watch duration. |
Sample Data
| content_id | genre |
|---|---|
| 1 | Drama |
| 2 | Comedy |
| 3 | Drama |
| 4 | Action |
| content_id | hours_watched |
|---|---|
| 1 | 2.0 |
| 2 | 1.5 |
| 3 | 3.0 |
| 4 | 2.5 |
| 1 | 1.0 |
| 2 | 2.0 |
| 3 | 2.5 |
| 4 | 1.5 |
| genre | total_hours |
|---|---|
| Drama | 8.5 |
SQL Editor
| genre | total_hours |
|---|---|
| Drama | 8.5 |
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 c.genre, SUM(w.hours_watched) AS total_hours FROM watching_activity w JOIN content c ON w.content_id = c.content_id GROUP BY c.genre ORDER BY total_hours DESC LIMIT 1;
Explanation
Attach each watch row to its content genre.
Sum watch time for every genre.
Sort totals descending and return the top genre.
Related Questions