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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:20:30+00:00 2026-06-05T05:20:30+00:00

I want to pull back results from one table that match ALL specified values

  • 0

I want to pull back results from one table that match ALL specified values where the specified values are in another table. I can do it like this:

SELECT * FROM Contacts 
WHERE 
EXISTS (SELECT 1 FROM dbo.ContactClassifications WHERE ContactID = Contacts.ID AND ClassificationID =  '8C62E5DE-00FC-4994-8127-000B02E10DA5')
AND EXISTS (SELECT 1 FROM dbo.ContactClassifications WHERE ContactID = Contacts.ID AND ClassificationID =  'D2E90AA0-AC93-4406-AF93-0020009A34BA')
AND EXISTS etc...

However that falls over when I get up to about 40 EXISTS clauses. The error message is “The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query.”

  • 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-05T05:20:31+00:00Added an answer on June 5, 2026 at 5:20 am

    The gist of this is to

    • Select all contacts with any GUID from the IN statement
    • Use a DISTINCT COUNT to get a count for each contactid on matching GUID’s
    • Use the HAVING to retain only those contacts that equal the amount of matching GUID’s you’ve put into the IN statement

    SQL Statement

    SELECT *
    FROM   dbo.Contacts c
           INNER JOIN (
              SELECT c.ID
              FROM   dbo.Contacts c
                     INNER JOIN dbo.ContactClassifications cc ON c.ID = cc.ContactID
              WHERE  cc.ClassificationID IN ('..', '..', 38 other GUIDS)
              GROUP BY
                     c.ID
              HAVING COUNT(DISTINCT cc.ClassificationID) = 40
           ) cc ON cc.ID = c.ID
    

    Test script at data.stackexchange

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

Sidebar

Related Questions

I'm using $.grep() to pull back a set of JSON results like so: myObject.appJSON
I am using Flot to chart some data that I pull back from the
Need some help: I want to only pull back the results for a specific
I'm stumped on casbah find. I'm trying to pull back all documents from a
I'm trying to figure out how pull select values from a table column, but
I want to pull back information about a loan. One piece of information is
I want to pull data from a database to display into a ComboBox, and
I have a remote monotone repos I want to pull from, but I really
I have a multidocument application. I want to pull data from each of those
I've forked a Mercurial repository, and now I want to pull the changes from

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.