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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:22:51+00:00 2026-05-16T16:22:51+00:00

This question reminded me of a couple related problems with whole-set comparison. Given: a

  • 0

This question reminded me of a couple related problems with whole-set comparison. Given:

  1. a collection of sets, and
  2. a probe set

Three questions:

  1. How do you find all sets in collection that match probe, element for element?
  2. How do you find all sets in collection that match a collection of probes, without the use of explicit looping constructs? How do you join sets of sets?
  3. Is this relational division? If not, what is it?

I have a decent solution to question 1 (see below).

I don’t have a decent relational solution to question 2. Any takers?

Test data:

IF OBJECT_ID('tempdb..#elements') IS NOT NULL DROP TABLE #elements
IF OBJECT_ID('tempdb..#sets') IS NOT NULL DROP TABLE #sets

CREATE TABLE #sets (set_no INT, PRIMARY KEY (set_no))
CREATE TABLE #elements (set_no INT, elem CHAR(1), PRIMARY KEY (set_no, elem))

INSERT #elements VALUES (1, 'A')
INSERT #elements VALUES (1, 'B')
INSERT #elements VALUES (1, 'C')
INSERT #elements VALUES (1, 'D')
INSERT #elements VALUES (1, 'E')
INSERT #elements VALUES (1, 'F')
INSERT #elements VALUES (2, 'A')
INSERT #elements VALUES (2, 'B')
INSERT #elements VALUES (2, 'C')
INSERT #elements VALUES (3, 'D')
INSERT #elements VALUES (3, 'E')
INSERT #elements VALUES (3, 'F')
INSERT #elements VALUES (4, 'B')
INSERT #elements VALUES (4, 'C')
INSERT #elements VALUES (4, 'F')
INSERT #elements VALUES (5, 'F')

INSERT #sets SELECT DISTINCT set_no FROM #elements

Setup and solution for question 1, set lookup:

IF OBJECT_ID('tempdb..#probe') IS NOT NULL DROP TABLE #probe
CREATE TABLE #probe (elem CHAR(1) PRIMARY KEY (elem))
INSERT #probe VALUES ('B')
INSERT #probe VALUES ('C')
INSERT #probe VALUES ('F')

-- I think this works.....upvotes for anyone who can demonstrate otherwise
SELECT set_no FROM #sets s
WHERE NOT EXISTS (
  SELECT * FROM #elements i WHERE i.set_no = s.set_no AND NOT EXISTS (
    SELECT * FROM #probe p WHERE p.elem = i.elem))
AND NOT EXISTS (
  SELECT * FROM #probe p WHERE NOT EXISTS (
    SELECT * FROM #elements i WHERE i.set_no = s.set_no AND i.elem = p.elem))

Setup for question 2, no solution:

IF OBJECT_ID('tempdb..#multi_probe') IS NOT NULL DROP TABLE #multi_probe
CREATE TABLE #multi_probe (probe_no INT, elem CHAR(1) PRIMARY KEY (probe_no, elem))
INSERT #multi_probe VALUES (1, 'B')
INSERT #multi_probe VALUES (1, 'C')
INSERT #multi_probe VALUES (1, 'F')
INSERT #multi_probe VALUES (2, 'C')
INSERT #multi_probe VALUES (2, 'F')
INSERT #multi_probe VALUES (3, 'A')
INSERT #multi_probe VALUES (3, 'B')
INSERT #multi_probe VALUES (3, 'C')

-- some magic here.....

-- result set:
-- probe_no | set_no
------------|--------
-- 1        | 4
-- 3        | 2
  • 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-16T16:22:52+00:00Added an answer on May 16, 2026 at 4:22 pm

    OK, let’s solve question 2 step by step:

    (1) Inner join sets and probes on their individual elements. This way we’ll see how do test sets and probe sets relate (which sets have what elements in common with which probe):

    SELECT
        e.set_no AS [test set],
        m.set_no AS [probe set],
        e.elem [common element]
    FROM
        @elements e
    JOIN
        @multi_probe m ON e.elem = m.elem
    

    Result:

    test set    probe set   common element
    ----------- ----------- --------------
    1           3           A
    1           1           B
    1           3           B
    1           1           C
    1           2           C
    1           3           C
    1           1           F
    1           2           F
    2           3           A
    2           1           B
    2           3           B
    2           1           C
    2           2           C
    2           3           C
    3           1           F
    3           2           F
    4           1           B
    4           3           B
    4           1           C
    4           2           C
    4           3           C
    4           1           F
    4           2           F
    5           1           F
    5           2           F
    

    (2) Count how many common elements between each test set and probe set (inner joins mean we already left the “no matches” aside)

    SELECT
        e.set_no AS [test set],
        m.set_no AS [probe set],
        COUNT(*) AS [common element count]
    FROM
        @elements e
        JOIN
            @multi_probe m ON e.elem = m.elem
    GROUP BY
        e.set_no, m.set_no
    ORDER BY
        e.set_no, m.set_no
    

    Result:

     test set    probe set   common element count
    ----------- ----------- --------------------
    1           1           3
    1           2           2
    1           3           3
    2           1           2
    2           2           1
    2           3           3
    3           1           1
    3           2           1
    4           1           3
    4           2           2
    4           3           2
    5           1           1
    5           2           1
    

    (3) Bring the counts of the test set and probe set on each row (subqueries may not be the most elegant)

    SELECT
        e.set_no AS [test set],
        m.set_no AS [probe set],
        COUNT(*) AS [common element count],
        (SELECT COUNT(*) FROM @elements e1 WHERE e1.set_no = e.set_no) AS [test set count],
        (SELECT COUNT(*) FROM @multi_probe m1 WHERE m1.set_no = m.set_no) AS [probe set count]
    FROM
        @elements e
        JOIN @multi_probe m ON e.elem = m.elem
    GROUP BY
        e.set_no, m.set_no
    ORDER BY
        e.set_no, m.set_no
    

    Result:

    test set    probe set   common element count test set count probe set count
    ----------- ----------- -------------------- -------------- ---------------
    1           1           3                    6              3
    1           2           2                    6              2
    1           3           3                    6              3
    2           1           2                    3              3
    2           2           1                    3              2
    2           3           3                    3              3
    3           1           1                    3              3
    3           2           1                    3              2
    4           1           3                    3              3
    4           2           2                    3              2
    4           3           2                    3              3
    5           1           1                    1              3
    5           2           1                    1              2
    

    (4) Find the solution: only retain those test sets and probe sets that have the same number of elements AND this number is also the number of common elements, i.e. the test set and the probe set are identical

    SELECT
        e.set_no AS [test set],
        m.set_no AS [probe set]
    FROM
        @elements e
    JOIN
        @multi_probe m ON e.elem = m.elem
    GROUP BY
        e.set_no, m.set_no
    HAVING
        COUNT(*) = (SELECT COUNT(*) FROM @elements e1 WHERE e1.set_no = e.set_no)
        AND (SELECT COUNT(*) FROM @elements e1 WHERE e1.set_no = e.set_no) = (SELECT COUNT(*) FROM @multi_probe m1 WHERE m1.set_no = m.set_no)
    ORDER BY
        e.set_no, m.set_no
    

    Result:

    test set    probe set
    ----------- -----------
    2           3
    4           1
    

    Excuse the @s instead of #s, I like table variables better 🙂

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

Sidebar

Related Questions

I saw this question and it reminded me of AutoGenerateColumns in the old DataGrid.
This question is directly related to this SO question I posed about 15 minutes
This question is related to another question I wrote: Trouble using DOTNET from PHP.
I recently saw some code that reminded me to ask this question. Lately, I've
I just came across this question in the Go FAQ, and it reminded me
This question is related to hooking with Mobile Substrate, but as long as you
This question is very similar to https://stackoverflow.com/questions/6839516/outlook-2010-reopen-messages which was closed as not being a
After reading this question , I was reminded of when I was taught Java
After reading this interesting question I was reminded of a tricky interview question I
This question is kind of a follow up to this question I asked a

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.