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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:40:33+00:00 2026-06-06T21:40:33+00:00

I have this table —————- | X | Y | —————- | a |

  • 0

I have this table

 ----------------
|   X    |   Y   |
 ----------------
|   a    |   1   |
|   c    |   6   |
|   e    |   3   |
|   d    |   6   |
|   c    |   4   |
|   b    |   1   |
|   a    |   5   |
|   g    |   1   |
 ----------------

When I’m given an array [c,d] I need to find “6” in the table above. I.e. for every set of elements I need to find the Y value that is shared by all the elements in the set but only if there is no other element (i.e. an element that’s not in the given array) that shares that value. The number of elements in the array has no theoretical limits.

More examples: for [a,b,c] I need to find nothing. For [a,b] I also need to find nothing (because g also has an entry for Y = 1, so for [a,b,g] I do need to find “1”).

I could of course iterate over the array, query by query, but that seems such an inefficient way of doing it. What’s the best way of doing this in SQL? Thank you.

  • 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-06T21:40:34+00:00Added an answer on June 6, 2026 at 9:40 pm

    Here’s a way by having your “query” values in a separate table.

    create table t ( x varchar(1), y int);
    
    insert into t (x, y) values ('a', 1);
    insert into t (x, y) values ('c', 6);
    insert into t (x, y) values ('e', 3);
    insert into t (x, y) values ('d', 6);
    insert into t (x, y) values ('c', 4);
    insert into t (x, y) values ('b', 1);
    insert into t (x, y) values ('a', 5);
    insert into t (x, y) values ('g', 1);
    
    create table q ( x varchar(1) );
    
    insert into q (x) values ('a');
    insert into q (x) values ('b');
    
    select a.y from
    (
       select t.y
         from t join q on (t.x = q.x)
       group by t.y
       having count(*) = (select count(*) from q)
    ) a 
    join t on (a.y = t.y) 
    group by a.y
    having count(*) = (select count(*) from q)
    

    Here’s an example SQLFiddle.

    This assumes that you can’t have duplicate combinations.

    If you want to do it without the second table, you can replace the select count(*) with the number of values you are matching to in an IN list, and instead of doing the join on the inner subquery, use a where clause.

    select a.y from
    (
    select t.y
    from t
      where t.x in ('c', 'd')
    group by t.y
    having count(*) = 2
    ) a 
    join t on (a.y = t.y) 
    group by a.y
    having count(*) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table that needs to have (for every row) the average cost
I have this table UMS id_attribute value order 1 MB 1 1 Gb 2
I have this table. I want to select back the items that don't exist
I have this table that has a lot of book related fields including keywords.
I have this table : And I would like to make a request that
I have this table ID Level Value 1 1 10 1 2 20 1
I have this table, from which i'd like to select all the td elements
I have this table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have this table Id PId Name Value ------------------------ 1 null foo null 2
i have this table of comments (little over 1 milion rows) that gets around

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.