Find Followers Count - Problem
Social Media Followers Analysis
You're working as a data analyst for a popular social media platform! Your task is to help the platform understand user engagement by calculating how many followers each user has.
You have access to the
Table Structure:
Your Mission: Write a SQL query that returns each user and their total follower count, ordered by user_id in ascending order.
Example: If user 123 has 5 people following them, your result should show
You're working as a data analyst for a popular social media platform! Your task is to help the platform understand user engagement by calculating how many followers each user has.
You have access to the
Followers table which records every follower relationship on the platform. Each row represents one user following another user.Table Structure:
| Column Name | Type | Description |
|---|---|---|
| user_id | int | The ID of the user being followed |
| follower_id | int | The ID of the user who is following |
Your Mission: Write a SQL query that returns each user and their total follower count, ordered by user_id in ascending order.
Example: If user 123 has 5 people following them, your result should show
user_id: 123, followers_count: 5 Input & Output
basic_followers.sql โ Basic Case
$
Input:
Followers table:
+--------+-----------+
|user_id |follower_id|
+--------+-----------+
| 1 | 101 |
| 2 | 102 |
| 1 | 103 |
| 2 | 104 |
| 1 | 105 |
+--------+-----------+
โบ
Output:
+--------+----------------+
|user_id |followers_count |
+--------+----------------+
| 1 | 3 |
| 2 | 2 |
+--------+----------------+
๐ก Note:
User 1 has 3 followers (101, 103, 105) and User 2 has 2 followers (102, 104). Results are ordered by user_id ascending.
single_follower.sql โ Single Followers
$
Input:
Followers table:
+--------+-----------+
|user_id |follower_id|
+--------+-----------+
| 10 | 20 |
| 11 | 21 |
| 12 | 22 |
+--------+-----------+
โบ
Output:
+--------+----------------+
|user_id |followers_count |
+--------+----------------+
| 10 | 1 |
| 11 | 1 |
| 12 | 1 |
+--------+----------------+
๐ก Note:
Each user has exactly one follower. The GROUP BY counts one row per user_id.
popular_user.sql โ One Very Popular User
$
Input:
Followers table:
+--------+-----------+
|user_id |follower_id|
+--------+-----------+
| 1 | 100 |
| 1 | 101 |
| 1 | 102 |
| 1 | 103 |
| 1 | 104 |
| 5 | 200 |
+--------+-----------+
โบ
Output:
+--------+----------------+
|user_id |followers_count |
+--------+----------------+
| 1 | 5 |
| 5 | 1 |
+--------+----------------+
๐ก Note:
User 1 is very popular with 5 followers, while User 5 has only 1 follower. This tests the aggregation with uneven distribution.
Visualization
Tap to expand
Understanding the Visualization
1
Scan Sign-up Sheet
Read through the follower relationships once
2
Group by Celebrity
Organize fans by which celebrity they follow
3
Count Each Group
Count fans in each celebrity group
4
Sort by Celebrity ID
Order results by celebrity ID ascending
Key Takeaway
๐ฏ Key Insight: SQL's GROUP BY is like having a super-efficient assistant who can sort and count in one pass, making follower analytics blazingly fast!
Time & Space Complexity
Time Complexity
O(n)
Single scan through all n rows in the table, plus O(n log n) for sorting by user_id
โ Linear Growth
Space Complexity
O(k)
Where k is the number of distinct users (for storing group counts and sorting)
โ Linear Space
Constraints
- 1 โค user_id, follower_id โค 109
- 1 โค number of rows in Followers table โค 104
- Each (user_id, follower_id) pair is unique
- A user cannot follow themselves (user_id โ follower_id)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code