Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3607520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:23:09+00:00 2026-05-18T21:23:09+00:00

Merry christmas everyone, I Know my way around SQL but I’m having a hard

  • 0

Merry christmas everyone,

I Know my way around SQL but I’m having a hard time figuring this one out.

First here are my tables (examples)

User

id
name

friend

 from //userid
 to //userid

If user 1 is friend with user 10
then you a row with 1,10.
User 1 cannot be friend with user 10 if user 10 is not friend with user 1
so you have
1,10
10,1

It may look weird but I need those two rows per relations.

Now I’m trying to make a query to select the users that have the most mutual friend with a given user.

For example User 1 is friend with user 10,9 and 7 and user 8 is friend with 10,9 and 7 too ,I want to suggest user 1 to invite him (like facebook).

I want to get like the 10 first people with the most mutual friend.

The output would be like

User,NumOfMutualFriends

I dont know if that can be done in a single query ?

Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:23:10+00:00Added an answer on May 18, 2026 at 9:23 pm
    SELECT u1.id, COUNT(*) FROM
    user as u1, user as u2, friend as f1, friend as f2
    WHERE u1.id = f1.to AND u2.id = f1.from AND -- f1 and f2 are friends 
    u2.id = f2.to AND f2.from = 1234 -- and f2 is my friend, so I have a mutual friend with f1
    GROUP BY u1.id
    

    EDIT1:
    This is just a general idea. Note that the count will be inaccurate, there will be duplicates. You’d have to play with some real data to tweak the query to eliminate all the duplicates (or find a real DBA)

    EDIT2:
    Also I suspect that this is probably not realistic as you are essentially doing a join of with at least n^4 results for the number of users. Which will get pretty bad even if you have 100 of them. So I’m pretty sure Facebook is doing something interesting. No idea what.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string 'Merry Christmas'. I want to separate this one as 'Merry'
Merry Christmas everyone ! I've installed Team Foundation Server 2010 with advanced configuration but
First of all, Merry Christmas to everyone! Now to my question: Let's say I
First of all Merry Christmas to everyone!!! Currently I have a NSArray that has
Merry Christmas everyone. I do have a question before Santa arrives. I'm new with
I have this problem for a very long time, but we are about to
I'm having an issue with a simple PHP mailer. I've had this script working,
First of all, Merry Christmas all! What I've been trying to do is real
I'm working on a small project with one other developer and we're having a
Merry Christmas!!! I've ran into a little issue and I need help. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.