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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:21:18+00:00 2026-05-12T17:21:18+00:00

I have the following tables: USERS table id username email password status FRIENDS table

  • 0

I have the following tables:

USERS table

  • id
  • username
  • email
  • password
  • status

FRIENDS table

  • id
  • user_id
  • friend_id
  • created

To find a users friends, I use:

SELECT f.friend_id, 
       u.username
  FROM friends f
  JOIN friends m ON m.user_id = f.friend_id 
                AND m.friend_id = f.user_id
  JOIN users u ON u.user_id = f.friend_id
 WHERE f.user_id = 1234

…which works great.

ISSUE

I can’t figure out a similar query in the other way though, find all the users that aren’t friends yet. It would have to be something along the lines of, get all the friends, get all the users, remove all users that are the friends to leave the remainder?

  • 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-12T17:21:18+00:00Added an answer on May 12, 2026 at 5:21 pm
        SELECT u1.username, u2.username AS not_friend
          FROM users u1
          JOIN users u2 on u1.user_id != u2.user_id
     LEFT JOIN friends f1 on u1.user_id = f1.user_id
                         and u2.user_id = f1.friend_id
     LEFT JOIN friends f2 on u2.user_id = f2.user_id 
                         and u1.user_id = f2.friend_id
         WHERE u1.user_id = 1234
           AND (f1.user_id IS NULL OR f2.user_id IS NULL)
    

    for friends:

    SELECT u1.username, u2.username AS friend
      FROM users u1
      JOIN users u2 on u1.user_id != u2.user_id
      JOIN friends f1 on u1.user_id = f1.user_id
                     and u2.user_id = f1.friend_id
      JOIN friends f2 on u2.user_id = f2.user_id 
                     and u1.user_id = f2.friend_id
     WHERE u1.user_id = 1234
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a users table with the following information users (id, username, password, email,
I have the following tables: team_members userid teamid users uid name_f name_l friends friend_id
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
Let's say the following indexes my username and email colours of my users table
I have the following table in the database: id, username, email, old_ip, current_ip Everytime
I have a table with the following fields: email - name - username -
I have the following tables in MySQL: users profile rates In the users table,
I have two tables like User and UserInfo UsersInfo UserId UserName Email Password LastLoginDate
I have the following two tables: auth_user id username is_active (boolean) ... useprofile_userprofile id
In SQL Server 2008 I have a user table like the following: Userid Username

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.