Plays, listeners and hours by genre
The task
The content team wants one line of listening stats per genre. A play belongs to the genre of its track's artist.
Return:
genreplays: the number of rows inplayslisteners: how many different listeners played that genrehours: totalseconds_playeddivided by 3600, rounded to 1 decimal placeavg_seconds: the averageseconds_playedper play, rounded to 1 decimal place
Every genre that has at least one artist must appear. A genre with no plays shows 0 for plays, listeners and hours, and NULL for avg_seconds.
Sort by plays descending, then genre ascending.
Expected output
genreplayslistenershoursavg_seconds- Sorted by plays descending, then genre ascending.
- Numbers rounded to 1 decimal place.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Music streaming service · click a name to insert it
listeners180 rows in the sample
One row per listener account. Key: listener_id.
| int | Unique id. | |
| date | Day they joined. | |
| text | Two-letter country code. | |
| text | free, premium or family. |
artists30 rows in the sample
One row per artist. Key: artist_id.
| int | Unique id. | |
| text | Name. | |
| text | pop, rock, hip-hop, electronic, jazz or folk. |
tracks116 rows in the sample
One row per track. Key: track_id.
| int | Unique id. | |
| text | Track title. | |
| int | Main artist. | |
| int | Length. | |
| date | Release day. |
plays6,555 rows in the sample
One row per play. Key: play_id.
| bigint | Unique id. | |
| int | Who listened. | |
| int | What. | |
| timestamp | Start time (UTC). | |
| int | How long they listened. | |
| text | phone, desktop, speaker or car. |
Related questions
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL