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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:27:46+00:00 2026-05-23T00:27:46+00:00

I have a conversation table, and a user conversation table. CONVERSATION Id, Subject, Type

  • 0

I have a conversation table, and a user conversation table.

CONVERSATION
Id, Subject, Type

USERCONVERSATION
Id, UserId, ConversationId

I need to do a SQL Query based on a list of UserIds. So, if I have three UserIds for the same ConversationId, I need to perform a query where if I provide the same three userIds, it will return the ConversationId where they match exactly.

  • 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-23T00:27:46+00:00Added an answer on May 23, 2026 at 12:27 am

    Assuming the same user can’t be in a UserConversation twice:

    SELECT ConversationID
    FROM UserConversation
    GROUP BY ConversationID
    HAVING
       Count(UserID) = 3 -- this isn't necessary but might improve performance
       AND Sum(CASE WHEN UserID IN (1, 2, 3) THEN 1 ELSE 0 END) = 3
    

    This also works:

    SELECT ConversationID
    FROM
       UserConversation UC
       LEFT JOIN (
          SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
       ) U (UserID) ON UC.UserID = U.UserID
    GROUP BY ConversationID
    HAVING
       Count(U.UserID) = 3
       AND Count(UC.UserID) = 3
    

    If you find that performance is poor with either of these queries then a two-step method could help: First find all conversations containing at least the desired parties, then from that set exclude those that contain any others. Indexes of course will make a big difference.

    Getting rid of the ID column from UserConversation will improve performance by getting more rows per page, thus more data per read (about 50% more!). If your Id column is not only the PK but also the clustered index, then immediately go change the clustered index to ConversationId, UserId (or vice versa, depending on the most common usage)!

    If you need help with performance post a comment and I’ll try to help you.

    P.S. Here’s another wild idea but it may not perform as well (though things can surprise you sometimes):

    SELECT
       Coalesce(C.ConversationID, UC.ConversationID) ConversationID
       -- Or could be Min(C.ConversationID)
    FROM
       Conversation C
       CROSS JOIN (
          SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
       ) U (UserID)
       FULL JOIN UserConversation UC
          ON C.ConversationID = UC.ConversationID
          AND U.UserID = UC.UserID
    GROUP BY Coalesce(C.ConversationID, UC.ConversationID)
    HAVING Count(*) = Count(U.UserID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User-Defined Table Type called tvpInsertedColumns : CREATE TYPE [Audit].[tvpInsertedColumns] AS TABLE(
i have a table which has a primary key (UserID) this is of type
I have a table that contains a GEOMETRY data type. SQL Server 2008 ships
Let say I have table Payments Id int autoincement Status int and my query
I have a query as follows; SELECT COUNT(Id) FROM Table The table contains 33
I have a query in my application that selects users from a table by
I have date range data in SQL DB table that has these three (only
I have a page where I display a list of threads for a user
I have a sql query that I return into a DataTable: SELECT TABLE_NAME, COLUMN_NAME,
Are there any standalone type conversion libraries? I have a data storage system that

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.