File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 1729 . Find Followers Count
2+
3+ + -- -----------+------+
4+ | Column Name | Type |
5+ + -- -----------+------+
6+ | user_id | int |
7+ | follower_id | int |
8+ + -- -----------+------+
9+ (user_id, follower_id) is the primary key (combination of columns with unique values ) for this table.
10+ This table contains the IDs of a user and a follower in a social media app where the follower follows the user.
11+
12+
13+ Write a solution that will, for each user, return the number of followers.
14+
15+ Return the result table ordered by user_id in ascending order.
16+
17+ The result format is in the following example.
18+
19+
20+
21+ Example 1 :
22+
23+ Input:
24+ Followers table:
25+ + -- -------+-------------+
26+ | user_id | follower_id |
27+ + -- -------+-------------+
28+ | 0 | 1 |
29+ | 1 | 0 |
30+ | 2 | 0 |
31+ | 2 | 1 |
32+ + -- -------+-------------+
33+ Output:
34+ + -- -------+----------------+
35+ | user_id | followers_count|
36+ + -- -------+----------------+
37+ | 0 | 1 |
38+ | 1 | 1 |
39+ | 2 | 2 |
40+ + -- -------+----------------+
41+ Explanation:
42+ The followers of 0 are {1 }
43+ The followers of 1 are {0 }
44+ The followers of 2 are {0 ,1 }
45+
46+
47+ # Write your MySQL query statement below
48+ SELECT user_id, COUNT (follower_id) AS followers_count
49+ FROM Followers
50+ GROUP BY user_id
51+ ORDER BY user_id;
You can’t perform that action at this time.
0 commit comments