Find Followers Count
Task: Write a solution that will, for each user, return the number of followers. Order the result by user_id.
Table: Followers
| Column Name | Type |
|---|---|
| user_id | INT |
| follower_id | INT |
Input: Followers table:
| user_id | follower_id |
|---|---|
| 0 | 1 |
| 1 | 0 |
| 2 | 0 |
| 2 | 1 |
Output:
| user_id | followers_count |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
View Reference Solution & Explanation ▼
This solution uses the `GROUP BY` clause to aggregate results to find the required output.
SELECT user_id, COUNT(follower_id) AS followers_count FROM Followers GROUP BY user_id ORDER BY user_id;