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
| 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.