Table Schema
Inspect
Table
| Column | Type |
|---|---|
| user_id | integer |
| song_id | integer |
Find users who listened to the same song more than once.
Table Schema
| Column | Type |
|---|---|
| user_id | integer |
| song_id | integer |
Sample Data
| user_id | song_id |
|---|---|
| 101 | 1 |
| 101 | 1 |
| 101 | 2 |
| 102 | 3 |
| 102 | 3 |
| 103 | 4 |
| user_id | song_id |
|---|---|
| 101 | 1 |
| 102 | 3 |
SQL Editor
| user_id | song_id |
|---|---|
| 101 | 1 |
| 102 | 3 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT user_id, song_id FROM streams GROUP BY user_id, song_id HAVING COUNT(*) > 1;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions