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

  • Home
  • SEARCH
  • 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 7056189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:50:16+00:00 2026-05-28T03:50:16+00:00

I have written this query which returns a users friends and the friends on

  • 0

I have written this query which returns a users friends and the friends on those friends. As there is a lot of sub queries going on here I thought there much be a more efficient way of writing it, but it’s a bit beyond me.

users table

++++++++++++++++++
+ user_id + name +
++++++++++++++++++
+ 1       + bill +
+ 2       + bob  +
+ 3       + sam  +
+ 4       + ben  +
++++++++++++++++++

user_friendships table

+++++++++++++++++++++++++++++++++++++
+ sender_user_id + receiver_user_id +
+++++++++++++++++++++++++++++++++++++
+ 1              + 2                +
+ 2              + 3                +
+ 4              + 2                +
+++++++++++++++++++++++++++++++++++++

The table is bi-directional so user 1 is a friend of user 2 and user 2 is a friend of user 1.

User 1 only has 1 friend user 2. User 2 has 2 friends, user 3 and user 4.

When the below query is run for user 1, user 2, 3 and 4 are returned.

query

SELECT * FROM users
WHERE ( user_id IN 

(SELECT receiver_user_id as user_id 
FROM user_friendships 
WHERE sender_user_id IN

(SELECT receiver_user_id as user_id 
FROM user_friendships 
WHERE sender_user_id = '1' 
UNION 
SELECT sender_user_id as user_id 
FROM user_friendships 
WHERE receiver_user_id = '1')

UNION

SELECT sender_user_id as user_id 
FROM user_friendships 
WHERE receiver_user_id IN

(SELECT receiver_user_id as user_id 
FROM user_friendships 
WHERE sender_user_id = '1' 
UNION 
SELECT sender_user_id as user_id 
FROM user_friendships 
WHERE receiver_user_id = '1')

)

OR user_id IN

(SELECT receiver_user_id as user_id 
FROM user_friendships 
WHERE sender_user_id = '1' 
UNION 
SELECT sender_user_id as user_id 
FROM user_friendships 
WHERE receiver_user_id = '1')
)
AND user_id != '1'

To clarify the end result of the friends and friends of friends query should be a single list of user_ids so that it can be joined with the user’s table to retrieve the name etc

  • 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-28T03:50:17+00:00Added an answer on May 28, 2026 at 3:50 am

    This could be helpfull: It tries to traverse the tables as few times as possible.

    -- First generation: Friends
    SELECT
      IF(firstgen.sender_user_id=<your-user-id>,firstgen.receiver_user_id,firstgen.sender_user_id) AS friend
    FROM
      user_friendships AS firstgen
    WHERE
      firstgen.receiver_user_id=<your-user-id>
      OR firstgen.sender_user_id=<your-user-id>
    
    UNION
    
    -- Second generation: Friends of friends
    SELECT
      IF(secondgen.sender_user_id in(firstgen.sender_user_id,firstgen.receiver_user_id),secondgen.receiver_user_id,secondgen.sender_user_id) AS friend
    FROM
      user_friendships AS firstgen
      INNER JOIN user_friendships AS secondgen ON
        (firstgen.sender_user_id=<your-user-id> AND (secondgen.sender_user_id=firstgen.receiver_user_id OR secondgen.receiver_user_id=firstgen.receiver_user_id))
      OR
        (firstgen.receiver_user_id=<your-user-id> AND (secondgen.sender_user_id=firstgen.sender_user_id OR secondgen.receiver_user_id=firstgen.sender_user_id))
    WHERE
      firstgen.receiver_user_id=<your-user-id>
      OR firstgen.sender_user_id=<your-user-id>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code which sends queries to google and returns the query
I have written a query which involves joins and finally returns the below result,
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have this linq query that works well (although it may be written better,
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a Google Instant style search script written in jQuery which queries a
I have written a query which works great on my local SQL Server 2005.
I have written this clone method for when the parent of the Employee class
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22

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.