Table Schema
Inspect
Table
| Column | Type |
|---|---|
| review_id | integer |
| listing_id | integer |
| rating | integer |
Find the average review score per listing.
Table Schema
| Column | Type |
|---|---|
| review_id | integer |
| listing_id | integer |
| rating | integer |
Sample Data
| review_id | listing_id | rating |
|---|---|---|
| 1 | 101 | 5 |
| 2 | 101 | 4 |
| 3 | 102 | 3 |
| 4 | 102 | 4 |
| 5 | 103 | 5 |
| 6 | 103 | 5 |
| listing_id | avg_rating |
|---|---|
| 101 | 4.50 |
| 102 | 3.50 |
| 103 | 5.00 |
SQL Editor
| listing_id | avg_rating |
|---|---|
| 101 | 4.50 |
| 102 | 3.50 |
| 103 | 5.00 |
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 listing_id, ROUND(AVG(rating), 2) AS avg_rating FROM reviews GROUP BY listing_id;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions