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 Followers table which records every follower relationship on the platform. Each row represents one user following another user.

Table Structure:
Column NameTypeDescription
user_idintThe ID of the user being followed
follower_idintThe 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
๐ŸŒŸ Celebrity Follower Count System๐Ÿ“‹ Sign-up SheetCelebrity | Fanโญ Taylor Swift | Fan101๐ŸŽฌ Tom Cruise | Fan102โญ Taylor Swift | Fan103๐ŸŽญ Lady Gaga | Fan104โญ Taylor Swift | Fan105๐ŸŽฌ Tom Cruise | Fan106๐Ÿ”„ GROUP BY MagicTaylor: 3Tom: 2Gaga: 1๐Ÿ† Final ResultsCelebrity | Countโญ Taylor Swift: 3๐ŸŽฌ Tom Cruise: 2๐ŸŽญ Lady Gaga: 1๐Ÿš€ SQL GROUP BY Process1. Single Scan: Read each follower relationship once2. Auto-Group: SQL automatically groups by celebrity (user_id)3. Count: COUNT(follower_id) gives us the magic numbers4. Sort: ORDER BY user_id for clean resultsโšก Time: O(n) | Space: O(k) where k = unique users
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of distinct users (for storing group counts and sorting)

n
2n
โœ“ 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)
Asked in
Meta 45 Instagram 38 Twitter 35 LinkedIn 25 TikTok 22
42.0K Views
High Frequency
~8 min Avg. Time
1.9K 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