-
Notifications
You must be signed in to change notification settings - Fork 0
/
Second Degree Follower 5-10-22
37 lines (29 loc) · 1.18 KB
/
Second Degree Follower 5-10-22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
link - https://www.codingninjas.com/codestudio/problems/second-degree-follower_2119337?topList=top-100-sql-problems&leftPanelTab=0
Problem Statement
In facebook, there is a follow table with two columns: followee, follower.
Please write a sql query to get the amount of each follower’s follower if he/she has one.
For example:
+-------------+------------+
| followee | follower |
+-------------+------------+
| A | B |
| B | C |
| B | D |
| D | E |
+-------------+------------+
Should output:
+-------------+------------+
| follower | num |
+-------------+------------+
| B | 2 |
| D | 1 |
+-------------+------------+
Explanation:
Both B and D exist in the follower list, when as a followee, B's follower is C and D, and D's follower is E. A does not exist in follower list.
Note:
Followee would not follow himself/herself in all cases.
Please display the result in follower's alphabet order.
----------------- solution ----------------------
select followee as follower ,count(follower) as num from follow
where followee in (select follower from follow)
group by 1