Table Schema
Inspect
Table
| Column | Type |
|---|---|
| stream_id | integer |
| user_id | integer |
| song_id | integer |
Find the top 3 most streamed songs.
Table Schema
| Column | Type |
|---|---|
| stream_id | integer |
| user_id | integer |
| song_id | integer |
Sample Data
| stream_id | user_id | song_id |
|---|---|---|
| 1 | 101 | 1 |
| 2 | 102 | 1 |
| 3 | 103 | 2 |
| 4 | 104 | 1 |
| 5 | 105 | 3 |
| 6 | 106 | 2 |
| 7 | 107 | 1 |
| 8 | 108 | 3 |
| 9 | 109 | 2 |
| song_id | stream_count |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
SQL Editor
| song_id | stream_count |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
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 song_id, COUNT(*) AS stream_count FROM streams GROUP BY song_id ORDER BY stream_count DESC LIMIT 3;
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