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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:53:05+00:00 2026-05-13T23:53:05+00:00

I am scratching my head to figure out a solution to the following question:

  • 0

I am scratching my head to figure out a solution to the following question:

I have a table with two fields, USER_ID and CLIENT_ID. For each USER_ID there are 1 to n CLIENT_IDs.

Let us say that user A is linked to clients 1,2 and 3. I want to construct a query that returns other users that also are linked to all of these clients. They may be linked to more clients, but they have to have links to all the clients of user A.

Example: User B has links to clients 1,2,3,4. User C has links to clients 1,2. The query should then return User B since User B has links to all of User A’s clients. User C should not be returned, since he only has links to some but not all of the clients of User A.

This seems like a deceptively simple problem, but I can’t for the life of me come up with a query that satisfies my constraints. Are the any experienced SQL gurus that can help me?

  • 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-13T23:53:06+00:00Added an answer on May 13, 2026 at 11:53 pm

    Making a number of name and datatype assumptions…

    DECLARE
      @UserId  int
     ,@ClientCount  int
    
    DECLARE @Clients as table
     (ClientId  int  not null)
    
    --  All clients for the "target" user
    INSERT @Clients
     select Clientid
     from MyTable
     where UserId = @userId
    
    --  Track how many there are
    SET @ClientCount = @@rowcount
    
    --  List all users that have those clients
    SELECT mt.UserId, count(*) HowMany
     from Mytable mt
      inner join @Clients cl
       on cl.ClientId = mt.Clientid
     where UserId <> @UserId
     group by mt.UserId
     having count(*) = @ClientCount
    

    I don’t have a table to test this against, but it should work with little debugging.

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

Sidebar

Related Questions

I have been scratching my head for hours trying to figure out why this
I've been scratching my head for hours trying to figure this out. I have
I have been scratching my head trying to figure out what is causing an
i'v been scratching my head for two hours in trying to figure out whats
I've been scratching my head trying to figure out how to use Regex.Replace to
I've been scratching my head for a while trying to figure this out. I
I have been scratching my head over this. I have two data frames: df
I'm scratching my head at this problem that I can't seem to figure out.
I have been scratching my head since long now to get rid of following
I am scratching my head to figure out a way to scale a signal

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.