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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:04:36+00:00 2026-05-11T22:04:36+00:00

Suppose I have a junction table EmployeeId DeptId ——— —— 1 1 1 2

  • 0

Suppose I have a junction table

EmployeeId  DeptId   
---------   ------
    1        1
    1        2
    1        3
    2        1
    2        2
    2        3   
    3        1
    3        2
    4        1
    5        2
    5        3
    6        1
    6        2
    6        3

So 1 employee can work in many departments

My problem is to find which employee works in multiple departments?

e.g.

If I want to search an employee who works for department 1,2,3, the result will be: 1,2,6

If I want to search an employee who works for department 2 & 3 the result will be 1,2,5,6

If I want to search an employee who works for department 1 & 2 the result will be 1,2 ,3,6

I tried with the following queries

a) SELECT DISTINCT EmployeeId FROM dbo.EmpDept WHERE DeptId in (2,3) 

I got wrong result

b) SELECT DISTINCT EmployeeId FROM dbo.EmpDept WHERE DeptId = 2 AND  DeptId = 3

This time I got no records

Please help me out.

N.B.~ I only simulated my real time project scenario. I cannot reveal the exact schema or table names or anything related to the project as it is confidential.

Thanks in advance

  • 1 1 Answer
  • 2 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-11T22:04:36+00:00Added an answer on May 11, 2026 at 10:04 pm

    this query will find all employees that work for more than 1 department.

    select employeeid, count(*) 
    from dbo.EmpDept 
    group by employeeid 
    having count(*) > 1;
    

    if you are hoping to gather data on the employees that cross between a set of specific EmpDepts, you can utilize self joins:

    select a.employeeid 
    from dbo.EmpDept a, dbo.EmpDept b
    where a.employeeid = b.employeeid 
      and a.deptid = 1
      and b.deptid = 2;
    

    using this method, you will have to add another join for every new department you are looking for.

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

Sidebar

Related Questions

suppose i have a windows service which does some work and developed by c#.
Suppose I have a table MYtable as given below: ID A B C 1
Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
Suppose I have a module which defines some basic constants such as integer, parameter
Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
Suppose I have a ASP.NET Web API controller that uses a service, which in
Suppose I have tables: Employee(empid, ....) Phone(phoneid, ...) EmployeePhone(empid, phoneid, ...) there is foreign
Suppose I have a FruitDetector class which takes in a string and returns the
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily

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.