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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:32:48+00:00 2026-05-20T23:32:48+00:00

I would like to query a relational database if a set of items exists.

  • 0

I would like to query a relational database if a set of items exists.

The data I am modeling are of the following form:

key1 = [ item1, item3, item5 ]
key2 = [ item2, item7 ]
key3 = [ item2, item3, item4, item5 ]
...

I am storing them in a table with the following schema

CREATE TABLE sets (key INTEGER, item INTEGER);

So for example, the following insert statements would insert the above three sets.

INSERT INTO sets VALUES ( key1, item1 );
INSERT INTO sets VALUES ( key1, item3 );
INSERT INTO sets VALUES ( key1, item5 );
INSERT INTO sets VALUES ( key2, item2 );
INSERT INTO sets VALUES ( key2, item7 );
INSERT INTO sets VALUES ( key3, item2 );
INSERT INTO sets VALUES ( key3, item3 );
INSERT INTO sets VALUES ( key3, item4 );
INSERT INTO sets VALUES ( key3, item5 );

Given a set of items, I would like the key associated with the set if it is stored in the table and NULL if it is not. Is it possible to do this with an sql query? If so, please provide details.

Details that may be relevant:

  • I am primarily interested in the database design / query strategy, though I will eventually implement this in MySQL and preform the query from with in python using the mysql-python package.
  • I have the freedom to restructure the database schema if a different layout would be more convenient for this type of query.
  • Each set, if it exists is supposed to be unique.
  • I am not interested in partial matches.
  • The database scale is on the order of < 1000 sets each of which contains < 10 items each, so performance at this point is not a priority.

Thanks in advance.

  • 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-20T23:32:48+00:00Added an answer on May 20, 2026 at 11:32 pm

    I won’t comment on whether there is a better suited schema for doing this (it’s quite possible), but for a schema having columns name and item, the following query should work. (mysql syntax)

    SELECT k.name
    FROM (SELECT DISTINCT name FROM sets) AS k
    INNER JOIN sets i1 ON (k.name = i1.name AND i1.item = 1)
    INNER JOIN sets i2 ON (k.name = i2.name AND i2.item = 3)
    INNER JOIN sets i3 ON (k.name = i3.name AND i3.item = 5)
    LEFT JOIN sets ix ON (k.name = ix.name AND ix.item NOT IN (1, 3, 5))
    WHERE ix.name IS NULL;
    

    The idea is that we have all the set keys in k, which we then join with the set item data in sets once for each set item in the set we are searching for, three in this case. Each of the three inner joins with table aliases i1, i2 and i3 filter out all set names that don’t contain the item searched for with that join. Finally, we have a left join with sets with table alias ix, which brings in all the extra items in the set, that is, every item we were not searching for. ix.name is NULL in the case that no extra items are found, which is exactly what we want, thus the WHERE clause. The query returns a row containing the set key if the set is found, no rows otherwise.


    Edit: The idea behind collapsars answer seems to be much better than mine, so here’s a bit shorter version of that with explanation.

    SELECT sets.name
    FROM sets
    LEFT JOIN (
        SELECT DISTINCT name
        FROM sets
        WHERE item NOT IN (1, 3, 5)
    ) s1
    ON (sets.name = s1.name)
    WHERE s1.name IS NULL
    GROUP BY sets.name
    HAVING COUNT(sets.item) = 3;
    

    The idea here is that subquery s1 selects the keys of all sets that contain items other that the ones we are looking for. Thus, when we left join sets with s1, s1.name is NULL when the set only contains items we are searching for. We then group by set key and filter out any sets having the wrong number of items. We are then left with only sets which contain only items we are searching for and are of the correct length. Since sets can only contain an item once, there can only be one set satisfying that criteria, and that’s the one we’re looking for.


    Edit: It just dawned on me how to do this without the exclusion.

    SELECT totals.name
    FROM (
        SELECT name, COUNT(*) count
        FROM sets
        GROUP BY name
    ) totals
    INNER JOIN (
        SELECT name, COUNT(*) count
        FROM sets
        WHERE item IN (1, 3, 5)
        GROUP BY name
    ) matches
    ON (totals.name = matches.name)
    WHERE totals.count = 3 AND matches.count = 3;
    

    The first subquery finds the total count of items in each set and the second one finds out the count of matching items in each set. When matches.count is 3, the set has all the items we’re looking for, and if totals.count is also 3, the set doesn’t have any extra items.

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

Sidebar

Related Questions

I would like to construct a query that displays all the results in a
I would like to use Linq to query a bus schedule in my project,
I would like to be able to query whether or not a service is
I would like to be able to do a query of a query to
I would like to convert the below foreach statement to a LINQ query that
Before trying to query the AD server I would like to check if it
I have a problem that I would like have solved via a SQL query.
I have a result from an SQL query that I would like to sort
For example, if I make a query like between(1,4,X)? I would expect something like
I'm using SQL Server 2005. The query would look like this Select col1, col2,

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.