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

  • Home
  • SEARCH
  • 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 6663507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:30:24+00:00 2026-05-26T02:30:24+00:00

I have 2 tables with many to many cardinality between them. So by normalization

  • 0

I have 2 tables with many to many cardinality between them. So by normalization I have created this :

   User
   UserId  UserName ....
     1       a
     2       b

  UserObject
  UserId  ObjectId
     1        1
     1        2
     2        2

  Object
  ObjectId  ObjectName
     1        c
     2        d

Now I want to run a query where I want to know users which have certain objects.

For example : All the users who have both objects c and d.

One way of doing it

   Select userid from UserObject where objectid=1 intersect Select userid from UserObject where objectid= 2

According to my use case, I may need to search for users having combination of 2-7 objects. It will not be prudent to write so many intersections.

I am working on postgesql 9.1.

What are the other efficient possible ways to make this happen?

  • 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-26T02:30:24+00:00Added an answer on May 26, 2026 at 2:30 am
    SELECT uo.UserId
        FROM UserObject uo
        WHERE uo.ObjectId IN (1,2)
        GROUP BY uo.UserId
        HAVING COUNT(DISTINCT uo.ObjectId) = 2
    

    Extending this concept for 7 objects:

    SELECT uo.UserId
        FROM UserObject uo
        WHERE uo.ObjectId IN (1,2,3,4,5,6,7)
        GROUP BY uo.UserId
        HAVING COUNT(DISTINCT uo.ObjectId) = 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database that contains many tables, some of them created dynamically
I have a user. A user can have many tables. Actual, only 5. In
I have 2 tables: Movies: movieID Users: userID These tables have a many to
Suppose I have many tables which have Foreign keys created. I want to get
If i have two tables like user table-u userid | name 1 | lenova
I have many tables in my database which are interrelated. I have a table
I have many tables that use Lookup/Enum references for most of their column values.
I have many tables with identical schemas but different names. Can I create a
I have 2 tables having many to many relation and hence there is junction
I have some tables that benefit from many-to-many tables. For example the team table.

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.