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 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
๐ŸŽต Spotify Artist Occurrence Counter๐Ÿ“€ Original Spotify Data๐ŸŽต "Shape of U" - Ed Sheeran๐ŸŽต "Blinding" - Weeknd๐ŸŽต "Levitating" - Dua Lipa๐ŸŽต "Bad Habits" - Ed Sheeran๐ŸŽต "Stay" - Bieber๐ŸŽต "Perfect" - Ed SheeranGROUP BY artist๐Ÿ“Š Grouped by Artist๐ŸŽค Ed SheeranShape of U, Bad Habits, Perfect๐ŸŽค WeekndBlinding๐ŸŽค Dua LipaLevitating๐ŸŽค BieberStayCOUNT + ORDER BY๐Ÿ† Final Ranking๐Ÿฅ‡ Ed Sheeran3 songs๐Ÿฅˆ Bieber1 song๐Ÿฅ‰ Dua Lipa1 songWeeknd1 song๐Ÿ’ก SQL Query:SELECT artist, COUNT(*) as occurrence_count FROM SpotifyGROUP BY artist ORDER BY occurrence_count DESC, artist ASC
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

n
2n
โšก Linearithmic
Space Complexity
O(k)

Space for k distinct artists in the result set

n
2n
โœ“ 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
Asked in
Spotify 85 Amazon 72 Netflix 68 Google 45 Meta 38 Apple 31
67.2K Views
High Frequency
~8 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen