my table has this structure:

 User_ID | Followed_ID

Example row:

 33 | 36

Means that user 33 follows user 36.

If exists a record like

 36 | 33

then the two users are considered friends. What's the cleanest way to get all friends of(for example) user 36? Thanks.

  • Are you using MySQL or SQL-Server? Feb 6, 2015 at 10:56
  • I'm using MySQL Feb 6, 2015 at 11:00
  • Then why did you tag with [sql-server]? Feb 6, 2015 at 11:07

1 Answer 1


It can be done with a simple join (assuming the (user_id, follower_id) combination has a unique constraint):

    f1.followed_id AS friend_id
    follow AS f1
    follow AS f2
      ON  f1.user_id = f2.followed_id
      AND f1.followed_id = f2.user_id
    f1.user_id = 36 ;     

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.