Select users belonging only to particular departments

后端 未结 12 2256
挽巷
挽巷 2021-02-18 12:49

I have the following table with two fields namely a and b as shown below:

create table employe
(
    empID varchar(10),
    department varchar(10)
);
         


        
12条回答
  •  灰色年华
    2021-02-18 13:46

    The following query works when you want employees from departments 'Y' and 'Z' and not 'X'.

    select empId from employe 
    where empId in (select empId from employe 
                    where department = 'Z') 
    and empId in (select empId from employe 
                  where department = 'Y') 
    and empId not in (select empId from employe 
                      where department = 'X') ;
    

    For your second case, simply replace not in with in in the last condition.

提交回复
热议问题