My Solution

  • 0

    To find the follower's follower, you could JOIN the table follow with itself on follower = followee.
    For example:

    A B
    B C
    B D
    D E


    A B
    B C
    B D
    D E

    You will get:

    A B B C
    A B B D
    B D D E

    Then you could calculate the sum.

    DISTINCT could be used to remove duplicated records after joining table

    SELECT tmp.followee AS follower,
             COUNT(tmp.followee) AS num
        (SELECT DISTINCT f1.follower AS followee,
             f2.follower AS follower
        FROM follow f1
        JOIN follow f2
            ON f1.follower = f2.followee) tmp
    GROUP BY  follower
    ORDER BY  follower;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.