SQL
Lab
Spotify Spotify Interview Question 01

Most
Streamed
Songs

Find the top 3 most streamed songs.

Table Schema

Inspect
Table

interactive
ColumnType
stream_idinteger
user_idinteger
song_idinteger

Sample Data

Input
Output

Sample Input: streams
stream_iduser_idsong_id
11011
21021
31032
41041
51053
61062
71071
81083
91092
Expected Output
song_idstream_count
14
23
32

SQL Editor

Run
Query

postgresql
Waiting for query

song_idstream_count
14
23
32

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT song_id, COUNT(*) AS stream_count
FROM streams
GROUP BY song_id
ORDER BY stream_count DESC
LIMIT 3;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving