Find users who have listened to every album by an artist

Quick Overview

Write a SQL query that finds, for each artist, the users who have listened to every one of that artist's albums, with repeat plays counted once. It tests relational division with GROUP BY and HAVING, distinct counts, and joining a listening log to album and artist tables.

Find users who have listened to every album by an artist

Company: Spotify

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For every artist, find the users who have listened to all of that artist's albums. Return `user_id`, `username`, `artist_name`, and the number of distinct albums the user listened to as `albums_listened`. Order by `artist_name` ascending, then `user_id` ascending. ```hint Watch what you count The listening history can hold many rows for the same user and album. Decide which number per user and artist has to match a number you can get from `albums` alone. ``` ### Input Table(s) `users` | column | type | meaning | |---|---|---| | user_id | INTEGER | User identifier | | username | VARCHAR | User's display name | Primary key: `user_id`. `artists` | column | type | meaning | |---|---|---| | artist_id | INTEGER | Artist identifier | | artist_name | VARCHAR | Artist's name | Primary key: `artist_id`. `artist_name` is unique. `albums` | column | type | meaning | |---|---|---| | album_id | INTEGER | Album identifier | | artist_id | INTEGER | Artist who released the album | | album_name | VARCHAR | Album title | Primary key: `album_id`. Foreign key: `artist_id` references `artists.artist_id`. `listening_history` | column | type | meaning | |---|---|---| | user_id | INTEGER | User who listened | | album_id | INTEGER | Album that was played | | listened_at | TIMESTAMP | When the listen happened | No primary key: the same `(user_id, album_id)` pair can appear many times. Foreign keys: `user_id` references `users.user_id`, `album_id` references `albums.album_id`. ### Output Contract | column | type | meaning | |---|---|---| | user_id | INTEGER | The user | | username | VARCHAR | The user's display name | | artist_name | VARCHAR | The artist whose albums the user has all heard | | albums_listened | INTEGER | Number of distinct albums by this artist the user listened to | Return one row per user and artist pair where the user has listened to every album by that artist. Order by `artist_name` ascending, then `user_id` ascending. Since `artist_name` is unique, this ordering fixes the position of every row. `albums_listened` is a whole number, so no rounding applies. ### Example `users` | user_id | username | |---|---| | 1 | ana | | 2 | ben | | 3 | cara | `artists` | artist_id | artist_name | |---|---| | 10 | Nova | | 20 | Echo | `albums` | album_id | artist_id | album_name | |---|---|---| | 100 | 10 | First Light | | 101 | 10 | Second Wind | | 200 | 20 | Static | `listening_history` | user_id | album_id | listened_at | |---|---|---| | 1 | 100 | 2024-03-01 08:10:00 | | 1 | 101 | 2024-03-01 09:00:00 | | 1 | 101 | 2024-03-02 18:45:00 | | 2 | 100 | 2024-03-03 12:00:00 | | 2 | 200 | 2024-03-03 12:40:00 | | 3 | 200 | 2024-03-04 07:30:00 | | 3 | 200 | 2024-03-05 07:30:00 | Expected output: | user_id | username | artist_name | albums_listened | |---|---|---|---| | 2 | ben | Echo | 1 | | 3 | cara | Echo | 1 | | 1 | ana | Nova | 2 | Nova has two albums. Ana heard both (one of them twice), so she qualifies with 2. Ben heard only First Light, so he is left out for Nova. Echo has one album, Static, and both Ben and Cara heard it; Cara's two plays still count as one album. Echo sorts before Nova, and the two Echo rows are ordered by `user_id`. ### Constraints and Clarifications - Row counts: `users` 1 to 10,000, `artists` 1 to 1,000, `albums` 1 to 10,000, `listening_history` 1 to 100,000. - Each `album_id` belongs to exactly one `artist_id`. - A user may listen to the same album many times, so `listening_history` has duplicate `(user_id, album_id)` rows. Repeat listens of one album count once. - All `user_id` and `album_id` values in `listening_history` are valid references, and none of the id columns are NULL. - A user who has heard some but not all of an artist's albums gets no row for that artist. Listens to other artists do not affect the result. - An artist with no albums produces no rows.

Overview: Write a SQL query that finds, for each artist, the users who have listened to every one of that artist's albums, with repeat plays counted once. It tests relational division with GROUP BY and HAVING, distinct counts, and joining a listening log to album and artist tables.

Spotify logo
Spotify
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For every artist, find the users who have listened to all of that artist's albums. Return user_id, username, artist_name, and the number of distinct albums the user listened to as albums_listened. Order by artist_name ascending, then user_id ascending.

Input Table(s)

users

columntypemeaning
user_idINTEGERUser identifier
usernameVARCHARUser's display name

Primary key: user_id.

artists

columntypemeaning
artist_idINTEGERArtist identifier
artist_nameVARCHARArtist's name

Primary key: artist_id. artist_name is unique.

albums

columntypemeaning
album_idINTEGERAlbum identifier
artist_idINTEGERArtist who released the album
album_nameVARCHARAlbum title

Primary key: album_id. Foreign key: artist_id references artists.artist_id.

listening_history

columntypemeaning
user_idINTEGERUser who listened
album_idINTEGERAlbum that was played
listened_atTIMESTAMPWhen the listen happened

No primary key: the same (user_id, album_id) pair can appear many times. Foreign keys: user_id references users.user_id, album_id references albums.album_id.

Output Contract

columntypemeaning
user_idINTEGERThe user
usernameVARCHARThe user's display name
artist_nameVARCHARThe artist whose albums the user has all heard
albums_listenedINTEGERNumber of distinct albums by this artist the user listened to

Return one row per user and artist pair where the user has listened to every album by that artist. Order by artist_name ascending, then user_id ascending. Since artist_name is unique, this ordering fixes the position of every row. albums_listened is a whole number, so no rounding applies.

Example

users

user_idusername
1ana
2ben
3cara

artists

artist_idartist_name
10Nova
20Echo

albums

album_idartist_idalbum_name
10010First Light
10110Second Wind
20020Static

listening_history

user_idalbum_idlistened_at
11002024-03-01 08:10:00
11012024-03-01 09:00:00
11012024-03-02 18:45:00
21002024-03-03 12:00:00
22002024-03-03 12:40:00
32002024-03-04 07:30:00
32002024-03-05 07:30:00

Expected output:

user_idusernameartist_namealbums_listened
2benEcho1
3caraEcho1
1anaNova2

Nova has two albums. Ana heard both (one of them twice), so she qualifies with 2. Ben heard only First Light, so he is left out for Nova. Echo has one album, Static, and both Ben and Cara heard it; Cara's two plays still count as one album. Echo sorts before Nova, and the two Echo rows are ordered by user_id.

Constraints and Clarifications

  • Row counts: users 1 to 10,000, artists 1 to 1,000, albums 1 to 10,000, listening_history 1 to 100,000.
  • Each album_id belongs to exactly one artist_id .
  • A user may listen to the same album many times, so listening_history has duplicate (user_id, album_id) rows. Repeat listens of one album count once.
  • All user_id and album_id values in listening_history are valid references, and none of the id columns are NULL.
  • A user who has heard some but not all of an artist's albums gets no row for that artist. Listens to other artists do not affect the result.
  • An artist with no albums produces no rows.