SQL
Lab
Spotify Spotify Interview Question 02

Artist
Popularity

Find the artist with the highest total streams.

Table Schema

Inspect
Table

interactive
ColumnType
song_idinteger
artist_idvarchar
ColumnType
stream_idinteger
song_idinteger

Sample Data

Input
Output

Sample Input: songs
song_idartist_id
1A1
2A2
3A1
4A3
Sample Input: streams
stream_idsong_id
11
21
32
43
53
63
74
Expected Output
artist_idtotal_streams
A15

SQL Editor

Run
Query

postgresql
Waiting for query

artist_idtotal_streams
A15

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 s.artist_id, COUNT(*) AS total_streams
FROM streams st
JOIN songs s ON st.song_id = s.song_id
GROUP BY s.artist_id
ORDER BY total_streams DESC
LIMIT 1;

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