Skip to main content

LeetCode 614. Second Degree Follower SQL Solution

Problem

LeetCode SQL Problem

  1. Second Degree Follower

follow table

followeefollower
AB
BC
BD
DE

Solution

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

followernum
B2
D1