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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:50:39+00:00 2026-05-27T04:50:39+00:00

I have 3 tables, User, Groups , and UserGroups. UserGroups hold the User id_user

  • 0

I have 3 tables, User, Groups , and UserGroups.

UserGroups hold the User id_user and Group id_group.

User has id_user, and Group has id_group.

My question would be:

How do I pull data from all 3 tables to achieve the following:

  1. Listing All Groups in Group Table
  2. Listing Groups in the UserGroup Table that has been selected
  3. Listing Users that ARE NOT in UserGroups in the User Table.
  4. Listing Users that belong to a Group in the UserGroup Table.

I have the following SQL statements, but I would wish to combine them into 1 sql statement. Is it possible??

select * from grupos

and

SELECT DISTINCT usuarios.id_usuario
FROM usuarios
WHERE usuarios.id_usuario
NOT IN (SELECT DISTINCT id_usuario FROM usuarios_grupos)

Whereby, the first query displays:

Groups
  1. John
  2. Tim
  3. Jane

The 2nd query shows the following:

User
 1. John
 2. Jane
 3. Tom

User Group
 Empty

After selection of John:

User
 2. Jane
 3. Tom

User Group
 1. John

Instead of having the following (which I have now):

User

 1. John
 2. Jane
 3. Tom

After Selection of John:

UserGroup

 1. John

Like what I have demonstrated above, if I select a Group and a User, and add it to User Groups. User Groups will display “Group, User” . (E.g. Development, Tom).

However, the Groups data table will still be shown as the following:

  1. Development
  2. Web
  3. Projects

The User data table will be “updated” instead:

  1. Tim
  2. Jane.

I tried the following, but I didn’t get the results I wanted:

SELECT grupos.id_grupo, usuarios.id_usuario
FROM  usuarios_grupos
INNER JOIN
grupos
ON
usuarios_grupos.id_grupo = grupos.id_grupo
INNER JOIN
usuarios
ON
usuarios_grupos.id_usuario = usuarios.id_usuario
WHERE
usuarios.id_usuario
NOT IN (SELECT DISTINCT id_usuario FROM usuarios_grupos)
  • 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-27T04:50:39+00:00Added an answer on May 27, 2026 at 4:50 am

    The simplest way to list the groups that have users belonging to them would be:

    SELECT DISTINCT id_grupo FROM usuarios_grupos
    

    The simplest way to list the groups that don’t have users belonging to them would be:

    SELECT DISTINCT id_grupo 
    FROM grupos
    WHERE id_grupo
    NOT IN (SELECT DISTINCT id_grupo FROM usuarios_grupos)
    

    EDIT: To list the Users that don’t belong in Groups, and all Groups that have Users in them, you can union the results of the queries that return each of these sets –

    SELECT DISTINCT id_usuario
    FROM usuarios
    WHERE id_usuario
    NOT IN (SELECT DISTINCT id_usuario FROM usuarios_grupos)
    UNION ALL
    SELECT DISTINCT id_grupo FROM usuarios_grupos
    

    Or you can do this in a single query:

    SELECT DISTINCT COALESCE(ug.id_grupo, u.id_usuario)
    FROM usuarios u
    LEFT JOIN usuarios_grupos ug ON u.id_usuario = ug.id_usuario
    

    (This last query assumes that there is no overlap between group and user ID sets.)

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

Sidebar

Related Questions

This question would be a bit childish, I have three tables, users, usergroups and
I'm setting up a group / user based security system. I have 4 tables
How would I query the following? User.name and Groups.description, from the User table and
I have a PostgreSQL database with two tables named user and group. They have
If I have users, as well as user groups (like a local astronomy group/club),
I have 3 tables called Users, Groups and UserGroups. UserGroups is the many 2
i have in the database typical tables like user, usergroup, and they have relations.
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 two models, user and group. I also have a joining table groups_users.
I have three tables: User: UserId (pk) FirstName Lastname Messages: MessageId (pk) Heading Body

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.