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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:00:33+00:00 2026-05-15T16:00:33+00:00

I have user and group tables set up as the following. users id int

  • 0

I have user and group tables set up as the following.

users
  id int

groups
  id int

users_groups
  user_id int
  group_id int

Example data:

+---------+----------+
| user_id | group_id |
+---------+----------+
| 1       | 1        |
| 1       | 2        |
| 2       | 1        |
| 3       | 2        |
| 3       | 3        |
| 4       | 1        |
| 4       | 2        |
| 5       | 2        |
+---------+----------+

Is there a way to select all users who are not in a given group? I tried joining the users and users_groups tables and adding a, say, group_id != 1 condition, but I end up getting the user when they are in another group, e.g., users [1, 3, 3, 4, 5].

So when I say I want users who are not in a given group, group_id != 1 in this case, the example results should be users [3, 5].

  • 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-15T16:00:33+00:00Added an answer on May 15, 2026 at 4:00 pm

    The simplest way to select the id of the users not in a given group is to use NOT IN:

    SELECT id
    FROM users
    WHERE id NOT IN (SELECT user_id FROM users_groups WHERE group_id = 1)
    

    Other commonly used alternatives are NOT EXISTS:

    SELECT id
    FROM users
    WHERE NOT EXISTS (
        SELECT NULL
        FROM users_groups
        WHERE group_id = 1
        AND user_id = users.id
    )
    

    And LEFT JOIN / IS NULL:

    SELECT id
    FROM users
    LEFT JOIN users_groups
    ON users.id = users_groups.user_id
    AND users_groups.group_id = 1
    WHERE users_groups.user_id IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have three tables: User -- contains users Group -- contains a set of
So I have 2 models. Users and Groups. Each group has a user as
I have the following entity: public class User { public int ID {get; set;}
I created 3 tables: User User-Group Group Where I can have a many-to-many relationship.
I'm setting up a group / user based security system. I have 4 tables
I have the following tables : Basically, a user is part of a project
I have the following data set, which represents nodes in a directed graph. CREATE
I have 2 tables with manyToMany relationship. user can have many groups and groups
I have two models, user and group. I also have a joining table groups_users.

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.