Similar Problems

Similar Problems not available

Second Degree Follower - Leetcode Solution

Companies:

LeetCode:  Second Degree Follower Leetcode Solution

Difficulty: Medium

Topics: database  

The Second Degree Follower problem on LeetCode is a SQL problem that challenges you to write a query that returns the second degree followers of a user given their ID. A second degree follower is someone who follows the followers of the user in question, but not the user themselves.

To solve this problem, we need to join 3 tables together: the followers table, the users table, and the follow table. The followers table stores which users follow which other users, while the users table stores the user's information, and the follow table stores information about the followers.

Here is the full solution to the Second Degree Follower problem on LeetCode:

SELECT DISTINCT f2.follower AS follower
FROM follow f1
JOIN follow f2 ON f1.followee = f2.follower
WHERE f1.follower = (SELECT user_id FROM users WHERE user_id = <user_id>)
AND f2.follower NOT IN (SELECT followee FROM follow WHERE follower = <user_id>)

In this solution, we first join the follow table with itself on the followee column of the f1 table and the follower column of the f2 table. This will give us a list of all the second degree followers.

Next, we add a WHERE clause that limits our results to the followers of the user in question. To do this, we compare the follower column of the f1 table to the user_id of the user we want to find second degree followers for. We also add a subquery to check that the user_id is valid.

Finally, we add another WHERE clause that excludes the first degree followers. To do this, we compare the followee column of the follow table to the user_id of the user we want to find second degree followers for.

Overall, this solution is fairly straightforward and uses basic SQL JOIN and WHERE clauses to find the second degree followers of a user on LeetCode.

Second Degree Follower Solution Code

1