Three tables describe artists, their songs and the Global chart: each chart_entries row is one song's position on one chart date.
Find the top 5 artists by number of Global Top 10 appearances (chart_position <= 10) across all their songs. Return artist_name and appearance_count, ordered by appearance_count descending, then artist_id ascending.
Input Table(s)
artists
| Column | Type | Meaning |
|---|
| artist_id | INT | Artist identifier |
| artist_name | VARCHAR(255) | Artist name, not NULL |
Primary key: artist_id.
songs
| Column | Type | Meaning |
|---|
| song_id | INT | Song identifier |
| song_title | VARCHAR(255) | Song title, not NULL |
| artist_id | INT | Artist who recorded the song |
Primary key: song_id. Foreign key: artist_id references artists.artist_id.
chart_entries
| Column | Type | Meaning |
|---|
| entry_id | INT | Chart entry identifier |
| song_id | INT | Song on the chart |
| chart_date | DATE | Date of the chart, not NULL |
| chart_position | INT | Position on that date's Global chart (1 is the top), not NULL |
Primary key: entry_id. Foreign key: song_id references songs.song_id.
Output Contract
Columns, in this order:
| Column | Definition |
|---|
| artist_name | The artist's name |
| appearance_count | Number of chart_entries rows with chart_position <= 10 for any of the artist's songs |
Order by appearance_count descending, then artist_id ascending, and return the first 5 rows of that order. artist_id is unique, so the order and the cut are fully determined.
Example
artists
| artist_id | artist_name |
|---|
| 1 | Nova Lane |
| 2 | The Static |
| 3 | Mira Cole |
| 4 | Echo Park Kids |
| 5 | Juno Reyes |
| 6 | Atlas Grey |
| 7 | Silver Fern |
songs
| song_id | song_title | artist_id |
|---|
| 101 | Glass Hearts | 1 |
| 102 | Midnight Run | 1 |
| 103 | Low Tide | 2 |
| 104 | Paper Planes | 3 |
| 105 | Northern Lights | 4 |
| 106 | Slow Burn | 5 |
| 107 | Coastline | 6 |
| 108 | Quiet Hours | 7 |
chart_entries
| entry_id | song_id | chart_date | chart_position |
|---|
| 1 | 101 | 2024-06-01 | 1 |
| 2 | 103 | 2024-06-01 | 2 |
| 3 | 104 | 2024-06-01 | 5 |
| 4 | 105 | 2024-06-01 | 7 |
| 5 | 102 | 2024-06-01 | 9 |
| 6 | 106 | 2024-06-01 | 10 |
| 7 | 108 | 2024-06-01 | 12 |
| 8 | 101 | 2024-06-08 | 3 |
| 9 | 103 | 2024-06-08 | 4 |
| 10 | 105 | 2024-06-08 | 6 |
| 11 | 107 | 2024-06-08 | 8 |
| 12 | 104 | 2024-06-08 | 11 |
| 13 | 102 | 2024-06-08 | 15 |
| 14 | 108 | 2024-06-08 | 50 |
Expected result:
| artist_name | appearance_count |
|---|
| Nova Lane | 3 |
| The Static | 2 |
| Echo Park Kids | 2 |
| Mira Cole | 1 |
| Juno Reyes | 1 |
Nova Lane gets 2 appearances from Glass Hearts and 1 from Midnight Run (its position 15 entry does not count). The Static and Echo Park Kids tie at 2 and are ordered by artist_id. Mira Cole, Juno Reyes (position 10 counts) and Atlas Grey tie at 1, and Atlas Grey has the largest artist_id of the three, so it falls outside the top 5. Silver Fern never reached the Top 10.
Constraints and Clarifications
-
chart_entries
has at most 10,000 rows,
artists
at most 1,000 and
songs
at most 5,000.
-
chart_position
is between 1 and 200. Top 10 means positions 1 to 10, inclusive.
-
Every qualifying
chart_entries
row is one appearance. A song in the Top 10 on three dates counts three times, and the appearances of all of an artist's songs are added together.
-
Only artists with at least one Top 10 appearance are returned. If fewer than 5 artists qualify, return fewer rows.
-
Exactly 5 rows are returned when at least 5 artists qualify, even if the sixth artist has the same count as the fifth. The
artist_id
tiebreak decides who is in.
-
Two artists can share an
artist_name
. They are different artists and get separate rows.
-
A chart entry that cannot be matched to an artist (for example, a song with a NULL
artist_id
) is not credited to anyone.