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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:08:58+00:00 2026-05-31T17:08:58+00:00

I have a simple SQL relational model with a many to many relation. Here

  • 0

I have a simple SQL relational model with a many to many relation. Here is the composition table

 ___________________________
| object1_id  | object2_id  |
|---------------------------|

I would like to know all the object1 that are common to a set of object2. My basic feeling is to do a request like this

SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_1>
INTERSECT 
SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_2>

And if I have N object2 in the set, I’ll do N INTERSECT

SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_1>
INTERSECT 
SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_2>
...
INTERSECT 
SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_N>

However, it doesn’t look very optimized. Can you help me ? I am not really a SQL expert. I think I could use a JOIN to do that.

Sample

 ___________________________
| object1_id  | object2_id  |
|---------------------------|
|         10  |           1 |
|         11  |           1 |
|         10  |           2 |
|         12  |           2 |
|         10  |           3 |
|         11  |           3 |
|         13  |           3 |

Example

  • { object2_id set } => { expected object1_id }
  • { 1, 2 } => { 10 }
  • { 1, 3 } => { 10, 11 }
  • { 1, 2, 3 } => { 10 }
  • 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-31T17:08:59+00:00Added an answer on May 31, 2026 at 5:08 pm

    In terms of performance your query looks OK. Have you measured it to see if there really is a problem?

    If (object1_id, object2_id) is unique then you can write the query more concisely as follows:

    SELECT object1_id
    FROM composition
    WHERE object2_id IN (id1, id2, ..., id6)
    GROUP BY object1_id
    HAVING COUNT(*) = 6
    

    Note that the 6 is the number of provided IDs. This should be changed if a different number of IDs is provided. You would have to measure the actual performance on your dadta to see if this gives any speed increase.

    If you can’t assume uniqueness then this should work:

    SELECT object1_id
    FROM composition
    WHERE object2_id IN (id1, id2, ..., id6)
    GROUP BY object1_id
    HAVING COUNT(DISTINCT object2_id) = 6
    

    The most important thing though is to make sure you have appropriate indexes on your table! This is far more important than whether you write one query or the other.

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

Sidebar

Related Questions

I have a simple SQL 'Select' query, and I'd like to dump the results
I'm new to SQL and relational databases and I have what I would imagine
I have a simple model like class Interest < ActiveRecord::Base has_and_belongs_to_many :user_profiles end class
I have a question about sql relation conception. I have a simple example. A
I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as
I have a simple SQL query (using SqlCommand, SqlTransaction) in .NET 2.0 that returns
I have a pretty simple SQL I need to perform. I have a ProcessUser
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have a very very simple SQL string that MS Access doesn't seem to

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.