Skip to content

Listener device habits by plan

MediumFull bankConditional aggregationCTEs and subqueries16 min2 hidden datasets

The task

The product team wants to know, for each plan, how many listeners are active and how widely they use their devices. Count listeners, not plays.

Return one row per plan in listeners with:

  • plan
  • listeners: number of listeners on the plan, including those with no plays
  • active_listeners: listeners with at least one row in plays
  • car_listeners: listeners with at least one play on the car device lasting 30 seconds or more (seconds_played >= 30); shorter car plays are skips and do not count
  • broad_listeners: listeners who have played on 3 or more different devices

A plan with no active listeners shows 0 in the last three columns. Sort by plan ascending.

Expected output

planlistenersactive_listenerscar_listenersbroad_listeners
  • Sorted by plan 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