Skip to content

Free and paid listeners per genre

MediumFull bankJoinsConditional aggregation14 min2 hidden datasets

The task

The product team wants to know which genres reach people on the free plan and which reach paying people. A listener is paid when listeners.plan is 'premium' or 'family', and free when it is 'free'.

For every genre that has at least one artist, count the distinct listeners who played at least one track by an artist of that genre. A play counts whatever its length.

Return:

  • genre
  • free_listeners: distinct free listeners who played the genre, 0 if none
  • paid_listeners: distinct paid listeners who played the genre, 0 if none

Sort by genre.

Expected output

genrefree_listenerspaid_listeners
  • Sorted by genre ascending.
  • 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.

intUnique id.
dateDay they joined.
textTwo-letter country code.
textfree, premium or family.
artists30 rows in the sample

One row per artist. Key: artist_id.

intUnique id.
textName.
textpop, rock, hip-hop, electronic, jazz or folk.
tracks116 rows in the sample

One row per track. Key: track_id.

intUnique id.
textTrack title.
intMain artist.
intLength.
dateRelease day.
plays6,555 rows in the sample

One row per play. Key: play_id.

bigintUnique id.
intWho listened.
intWhat.
timestampStart time (UTC).
intHow long they listened.
textphone, desktop, speaker or car.

Related questions

Loading the editor and PostgreSQL