Create a Common Table Expression (CTE) to union the Friendship table with itself, but with user1_id and user2_id swapped, to account for the bidirectional nature of friendships.
Perform a self-join on this CTE to find pairs of users that have a common friend.
Group the results by the user pairs and count the number of common friends they have.
Use a HAVING clause to filter out pairs that have fewer than three common friends.
Select the user pairs and their count of common friends as the final result.
Ensure that the result does not contain duplicates by selecting pairs where user1_id is less than user2_id.
erDiagram
Friendship {
int user1_id
int user2_id
}
Alternative Approach Using Subquery for Common Friends