Table Schema
Inspect
Table
| Column | Type |
|---|---|
| song_id | integer |
| artist_id | varchar |
| Column | Type |
|---|---|
| stream_id | integer |
| song_id | integer |
Find the artist with the highest total streams.
Table Schema
| Column | Type |
|---|---|
| song_id | integer |
| artist_id | varchar |
| Column | Type |
|---|---|
| stream_id | integer |
| song_id | integer |
Sample Data
| song_id | artist_id |
|---|---|
| 1 | A1 |
| 2 | A2 |
| 3 | A1 |
| 4 | A3 |
| stream_id | song_id |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 3 |
| 6 | 3 |
| 7 | 4 |
| artist_id | total_streams |
|---|---|
| A1 | 5 |
SQL Editor
| artist_id | total_streams |
|---|---|
| A1 | 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 s.artist_id, COUNT(*) AS total_streams FROM streams st JOIN songs s ON st.song_id = s.song_id GROUP BY s.artist_id ORDER BY total_streams DESC LIMIT 1;
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