forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_614.sql
32 lines (31 loc) · 1.08 KB
/
_614.sql
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
--614. Second Degree Follower
--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 |
--+-------------+------------+
--Explaination:
--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.
select f1.follower, count(distinct f2.follower) as num
from follow f1
inner join follow f2 on f1.follower = f2.followee
group by f1.follower