Count Artist Occurrences On Spotify Ranking List - Problem
Count Artist Occurrences On Spotify Ranking List
You're working with Spotify's music ranking database and need to analyze artist popularity! ๐ต
Given a table called
Goal: Return a result showing each artist's name along with their occurrence count, ordered by:
1. Occurrence count in descending order (most popular first)
2. If counts are equal, order by artist name in ascending order (alphabetical)
Table Schema:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| track_name | varchar |
| artist | varchar |
+-------------+---------+
This is a classic SQL aggregation problem that tests your understanding of
You're working with Spotify's music ranking database and need to analyze artist popularity! ๐ต
Given a table called
Spotify with columns id, track_name, and artist, your task is to count how many times each artist appears in the ranking list.Goal: Return a result showing each artist's name along with their occurrence count, ordered by:
1. Occurrence count in descending order (most popular first)
2. If counts are equal, order by artist name in ascending order (alphabetical)
Table Schema:
Spotify+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| track_name | varchar |
| artist | varchar |
+-------------+---------+
This is a classic SQL aggregation problem that tests your understanding of
GROUP BY, COUNT, and ORDER BY clauses. Input & Output
example_1.sql โ Basic Spotify Rankings
$
Input:
Spotify table:
+----+------------+---------+
| id | track_name | artist |
+----+------------+---------+
| 1 | Shape of U | Ed Sheeran |
| 2 | Blinding | Weeknd |
| 3 | Levitating | Dua Lipa|
| 4 | Bad Habits | Ed Sheeran |
| 5 | Stay | Bieber |
| 6 | Perfect | Ed Sheeran |
+----+------------+---------+
โบ
Output:
+-----------+------------------+
| artist | occurrence_count |
+-----------+------------------+
| Ed Sheeran| 3 |
| Bieber | 1 |
| Dua Lipa | 1 |
| Weeknd | 1 |
+-----------+------------------+
๐ก Note:
Ed Sheeran appears 3 times (most frequent), so he's first. The other artists each appear once, so they're sorted alphabetically: Bieber, Dua Lipa, Weeknd.
example_2.sql โ Equal Occurrences
$
Input:
Spotify table:
+----+------------+---------+
| id | track_name | artist |
+----+------------+---------+
| 1 | Song A | Taylor |
| 2 | Song B | Adele |
| 3 | Song C | Taylor |
| 4 | Song D | Beyonce |
| 5 | Song E | Adele |
| 6 | Song F | Beyonce |
+----+------------+---------+
โบ
Output:
+-----------+------------------+
| artist | occurrence_count |
+-----------+------------------+
| Adele | 2 |
| Beyonce | 2 |
| Taylor | 2 |
+-----------+------------------+
๐ก Note:
All artists have the same count (2), so they're ordered alphabetically: Adele, Beyonce, Taylor.
example_3.sql โ Single Artist
$
Input:
Spotify table:
+----+------------+---------+
| id | track_name | artist |
+----+------------+---------+
| 1 | Hit Song | Drake |
+----+------------+---------+
โบ
Output:
+-----------+------------------+
| artist | occurrence_count |
+-----------+------------------+
| Drake | 1 |
+-----------+------------------+
๐ก Note:
Only one artist in the table, so the result contains just Drake with count 1.
Visualization
Tap to expand
Understanding the Visualization
1
Scan Table Once
SQL reads each row and groups by artist name
2
Count Groups
COUNT(*) tallies rows in each artist group
3
Sort Results
ORDER BY arranges by count (desc) then name (asc)
Key Takeaway
๐ฏ Key Insight: SQL's GROUP BY automatically handles the grouping and counting in a single efficient pass, making it the optimal solution for aggregation problems like this.
Time & Space Complexity
Time Complexity
O(n log n)
O(n) for grouping and counting, O(n log n) for sorting the result
โก Linearithmic
Space Complexity
O(k)
Space for k distinct artists in the result set
โ Linear Space
Constraints
- 1 โค Number of rows โค 104
- 1 โค id โค 109
- track_name and artist contain only printable ASCII characters
- track_name and artist lengths are between 1 and 100 characters
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code