AnalystPath

Genres Commonly Heard by the Same Listeners

SQLHardSenior level~15 min

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.

plays
listener_idtrack_idplay_count
11012
11021
12013
tracks
track_idgenrelength_sec
101Rock210
102Jazz180
103Jazz240

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

Related SQL questions