LeetCode 614. Second Degree Follower SQL Solution
Problem
LeetCode SQL Problem
- Second Degree Follower
follow table
| followee | follower |
|---|---|
| A | B |
| B | C |
| B | D |
| D | E |
Solution
- MySQL
- TSQL
Second Degree Follower
-- Use Inner Self Join to get the amount of each follower’s follower if he/she has one
SELECT A.follower
,count(DISTINCT B.follower) AS num
FROM follow AS A
INNER JOIN follow AS B ON A.follower = B.followee
GROUP BY A.follower
Second Degree Follower
-- Use Inner Self Join to get the amount of each follower’s follower if he/she has one
SELECT A.follower
,count(DISTINCT B.follower) AS num
FROM follow AS A
INNER JOIN follow AS B ON A.follower = B.followee
GROUP BY A.follower
Query Output
| follower | num |
|---|---|
| B | 2 |
| D | 1 |