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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:40:25+00:00 2026-06-18T08:40:25+00:00

Suppose I have a many-to-many relation between users and projects: one user may belong

  • 0

Suppose I have a many-to-many relation between users and projects: one user may belong to several projects and one project may have multiple users. This relation is encoded in table user_projects:

create table user_projects
(
proj_id int references projs(id) not null,
user_id int references users(id) not null,
primary key (proj_id, user_id)
);

Here’s my problem: given a set of users (user1, user2, …), I want to select all projects for which the given set of users is a subset of all its users.

If, for example, I were insert the data below and then ask for all projects with users 1 and 2, then the query should return only project 1.

insert into user_projects values (1, 1);
insert into user_projects values (1, 2);
insert into user_projects values (1, 3);
insert into user_projects values (2, 1);
insert into user_projects values (2, 3);

(I’m using PostgreSQL, if the best solution happens to be non-standard.)

EDIT: For clarification, the set of users should be interpreted as a constraint on the list of projects to return. The set {u1, u2} means that the list of projects should include only those projects having at least users u1 and u2; the set {u1} means that all projects having at least user u1 should be returned, and as a limiting case, the empty set means that all projects should be returned.

  • 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-06-18T08:40:26+00:00Added an answer on June 18, 2026 at 8:40 am
    Select project_ID 
    from user_projects
    where user_ID in (1,2)
    group by project_ID
    Having count(*) = 2
    

    You know that you have 2 users, you know that they will be unique (primary key)
    so you know that if there are 2 records, for the same project then it’s one you want.

    Your question indicated you have a GIVEN sent of users therefor you know what users and how many there are. the above SQL could be updated to accept parameters for these known and thus remains dynamic, not limited to just 2 users.

    where user_ID in (userlist)
    having count(*) = (cntuserList)
    

    ———–To handle situation when set of users is empty—–

    Select P.project_ID 
    from Projects P
    LEFT JOIN user_projects UP
    where (UP.user_ID in (1,2) OR UP.USER_ID is null)
    group by project_ID
    Having count(*) = 2
    

    So here’s what this does. It returns all projects and if there’s a user affiliated to that project it identifies them.
    If you set contains users, the list of projects returned is filtered by that set ensuring that the entire set is in the project through the having clause.

    If the set is empty, the LEFT join along with the userID is null statement will keep the projects with no users listed regardless of if the set is empty or not. The having clause will further reduce the set to the # of users you defined in the set, OR 0 indicating return all projects with no users assigned.

    One additional edge case we didn’t discuss yet is what should happen if a project contains more users than what you defined in the set. Presently this project would be returned; but i’m not positive that’s what you wanted.

    on a side note thanks for making me think. I don’t get to get into the code as much anymore; which is why I troll here from time to time to see if I can help!

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

Sidebar

Related Questions

I tried many ways to solve this problem but none works. suppose I have
Let's suppose I have two entities User and Product related by a Many-to-Many relationship
Suppose you have a disease diagnosis Prolog program that starts with many relations between
Suppose I have many elements, and isotope works perfectly. Now, when a user click
Please suppose I have many downloadable pictures (in example, one thousand JPG images) in
Suppose we have many user processes running on Linux. Each process has many threads
Suppose we have two models, Task and User. So a user can have many
can it make sense for one table to have multiple foreign keys? Suppose I
Suppose i have two tables with one to many relationship (association in EF 4.1)
Suppose on my HTML page I have many href s to a link. Using

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.