Schema
Table
Setup
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Reviewed content identifier. |
| rating | integer | Rating value. |
Find the content with the highest average rating.
Schema
| Column Name | Type | Description |
|---|---|---|
| content_id | integer | Reviewed content identifier. |
| rating | integer | Rating value. |
Sample Data
| content_id | rating |
|---|---|
| 1 | 4 |
| 1 | 5 |
| 1 | 3 |
| 2 | 3 |
| 2 | 4 |
| 3 | 2 |
| 3 | 3 |
| 4 | 5 |
| 4 | 4 |
| content_id | avg_rating |
|---|---|
| 4 | 4.5 |
SQL Editor
| content_id | avg_rating |
|---|---|
| 4 | 4.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 content_id, AVG(rating) AS avg_rating FROM reviews GROUP BY content_id ORDER BY avg_rating DESC LIMIT 1;
Explanation
Group review rows by content_id.
Calculate average rating for each content_id.
Return the highest-rated content.
Related Questions