Genres Commonly Heard by the Same Listeners
Problem
Table: `plays`
```text
+-------------+------+
| Column Name | Type |
+-------------+------+
| listener_id | int |
| track_id | int |
| play_count | int |
+-------------+------+
There is no single-column primary key. Each row records that a listener
played a track some number of times.
```
Table: `tracks`
```text
+-------------+---------------+
| Column Name | Type |
+-------------+---------------+
| track_id | int |
| genre | varchar |
| length_sec | decimal(10,2) |
+-------------+---------------+
track_id is the primary key for this table.
```
Find every pair of genres for which at least three distinct listeners played a track from both genres. Treat each pair once, with the alphabetically smaller genre first.
Return `genre1`, `genre2`, and `listener_count`, ordered by `listener_count` descending, then `genre1` ascending, then `genre2` ascending.
Tables
Example rows — the live problem includes the full dataset.
| listener_id | track_id | play_count |
|---|---|---|
| 1 | 101 | 2 |
| 1 | 102 | 1 |
| 1 | 201 | 3 |
| track_id | genre | length_sec |
|---|---|---|
| 101 | Rock | 210 |
| 102 | Jazz | 180 |
| 103 | Jazz | 240 |
Expected output
Your answer should return 4 rows with the columns genre1, genre2, listener_count.
Starter code (SQL)
SELECT *
FROM plays;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock