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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:56:10+00:00 2026-05-13T06:56:10+00:00

I have the following tables: Table: user_groups (many-to-many) user_id (int) group_id (varchar) Table: profile_groups

  • 0

I have the following tables:

Table: user_groups (many-to-many)

  • user_id (int)
  • group_id (varchar)

Table: profile_groups (many-to-many)

  • profile_id (int)
  • group_id (varchar)

So basically, I want to write a sql script to find out what profile is assigned to each user.

So in the end there should be only 2 columns: user_id and profile_id.

How would I go about doing this?

Edit: It’s actually a lot more complicated than a simple join.

E.g.

User_groups may have the following rows

  • 1 group1
  • 1 group2
  • 1 group3
  • 2 group1
  • 2 group2
  • 3 group4

and profile_groups may have the following:

  • 11 group1
  • 11 group2
  • 11 group3
  • 21 group1
  • 21 group2
  • 22 group4

So the result should be

  • 1 11
  • 2 21
  • 3 22

Each user should only have ONE profile

  • 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-13T06:56:11+00:00Added an answer on May 13, 2026 at 6:56 am

    I just saw a question like this the other day. I think the hard part here is you’re looking for user_id/profile_id combinations where the user_id has every group_id that the profile_id has, no more and no less. So take the usual join and add some correlation to count the number of group_ids each profile/user has and make sure they match (this has been edited a few times):

     select user_id, profile_id 
        from user_groups join profile_groups on 
        user_groups.group_id=profile_groups.group_id 
        group by user_id, profile_id
        having count(user_groups.group_id) = 
        (select count(*) from profile_groups as pg where 
        pg.profile_id=profile_groups.profile_id)
        and count(profile_groups.group_id) = (select count(*) from user_groups as ug where 
        ug.user_id=user_groups.user_id)
        ;
    

    Here’s a run which includes two profiles with three groups each, with one common group between them and a new user in the fourth profile:

    sqlite>  create table user_groups (user_id integer, group_id varchar);
    sqlite>  create table profile_groups (profile_id integer, group_id varchar);
    sqlite>  insert into user_groups values(1, 'group1');
    sqlite>  insert into user_groups values(1, 'group2');
    sqlite>  insert into user_groups values(1, 'group3');
    sqlite>  insert into user_groups values(2, 'group1');
    sqlite>  insert into user_groups values(2, 'group2');
    sqlite>  insert into user_groups values(3, 'group4');
    sqlite>  insert into user_groups values(4, 'group1');
    sqlite>  insert into user_groups values(4, 'group5');
    sqlite>  insert into user_groups values(4, 'group6');
    sqlite> 
    sqlite>  insert into profile_groups values (11, 'group1');
    sqlite>  insert into profile_groups values (11, 'group2');
    sqlite>  insert into profile_groups values (11, 'group3');
    sqlite> 
    sqlite>  insert into profile_groups values (21, 'group1');
    sqlite>  insert into profile_groups values (21, 'group2');
    sqlite> 
    sqlite>  insert into profile_groups values (22, 'group4');
    sqlite> 
    sqlite>  insert into profile_groups values (23, 'group1');
    sqlite>  insert into profile_groups values (23, 'group5');
    sqlite>  insert into profile_groups values (23, 'group6');
    sqlite>  select user_id, profile_id 
       ...>     from user_groups join profile_groups on 
       ...>     user_groups.group_id=profile_groups.group_id 
       ...>     group by user_id, profile_id
       ...>     having count(user_groups.group_id) = 
       ...>     (select count(*) from profile_groups as pg where 
       ...>     pg.profile_id=profile_groups.profile_id)
       ...>     and count(profile_groups.group_id) = (select count(*) from user_groups as ug where 
       ...>     ug.user_id=user_groups.user_id)
       ...>     ;
    1|11
    2|21
    3|22
    4|23
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table with data: t1 (results): card_id group_id project_id user_id The
I have the following table (simplified): user_id date hours 1 2012-03-01 5 2 2012-03-01
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have the following tweets table: tweet_id user_id text --------------------------------------------------- 1 2 this is
I currently have the following row in my table: course_data: user_id days <-- This
I have a table with user_id and lap_time and I'm running the following query:
I have the following tables - groups, contacts, contacts_groups (habtm join table) groups &
I have the following tables : Basically, a user is part of a project
I have 2 tables: users (id, firstname, lastname, etc) users_to_groups (user_id(index), group_id(index)) I would
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id

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.