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 610151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:38:38+00:00 2026-05-13T17:38:38+00:00

I have this problem. Given a users table that consists of users’ username in

  • 0

I have this problem. Given a users table that consists of users’ username in a social network and friends table that contain a user’s name and a user’s friendname like below…

username friendname

John        Thomas
Chris       James

… I’m trying to write an SQL statement that will if a user is in my network. In other words
is that user a friend or friend of friends?

I’ve been dancing around this problem and could only come up with this query:

SELECT f2.username, f2.friendname 
FROM friends f2 
WHERE f2.username IN (
      SELECT f1.friendname 
      FROM friends f1 
      WHERE f1.username = 'Thomas') 
AND f2.friendname <> 'user1' 
AND f2.friendname = 'user2';    

It basically check if a user if is a friend of my friend i.e. just return null if false.

Trying to figure out how I can expand to go through all my network of friend. I mean not just friend of my friend.

  • 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-13T17:38:39+00:00Added an answer on May 13, 2026 at 5:38 pm
    SELECT  *
    FROM    (
            SELECT  username
            FROM    friends
            START WITH
                    username = 'myname'
            CONNECT BY
                    friendname = PRIOR username
                    AND level <= 3
            )
    WHERE   username = 'friendname'
            AND rownum = 1
    

    Update the level as necessary: you may search for the third layer friends etc.

    If the friendship relationship is symmetric, you should make the following query:

    WITH    q AS
            (
            SELECT  username, friendname
            FROM    friends
            UNION ALL
            SELECT  friendname, username
            FROM    friends
            ),
            f AS
            (
            SELECT  friendname, level
            FROM    q
            START WITH
                    username = 'Thomas'
            CONNECT BY NOCYCLE
                    username = PRIOR friendname
            )
    SELECT  *
    FROM    f
    WHERE   friendname = 'Jo'
            AND rownum = 1
    

    This query can be made much faster if you denormalize your table: store two records per friendship, like this:

    CREATE TABLE dual_friends (orestes NOT NULL, pylades NOT NULL, CONSTRAINT pk_dualfriends_op PRIMARY KEY (orestes, pylades)) ORGANIZATION INDEX
    AS
    SELECT  username, friendname
    FROM    friends
    UNION ALL
    SELECT  friendname, username
            FROM    friends
    

    Then you can just replace the CTE above with the dual_friends:

    WITH    f AS
            (
            SELECT  pylades, level
            FROM    dual_friends
            START WITH
                    orestes  = 'Thomas'
            CONNECT BY NOCYCLE
                    orestes = PRIOR pylades
                    AND level <= 3
            )
    SELECT  *
    FROM    f
    WHERE   pylades = 'Jo'
            AND rownum = 1
    

    , which will use the index and be much more efficient, especially if you limit the level to some reasonable value.

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

Sidebar

Related Questions

No related questions found

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.