在接受采访时我被问到这个SQL问题。
给出员工表
LastName DepartmentID
Rafferty 31
Jones 33
Heisenberg 33
Robinson 34
Smith 34
Daniel 34
Williams NULL
和部门表
DepartmentID DepartmentName
31 Sales
33 Engineering
34 Clerical
35 Marketing
我怎样才能找到与罗宾逊在同一个部门的所有员工?'? (通过一般方式)
加入(内部联接)他希望我做的事情吗?
答案 0 :(得分:1)
您可以使用IN
:
select *
from employee
where departmentId in (
select departmentId
from employee
where LastName = 'Robinson'
);
使用JOIN
:
select e.*
from employee e
join (
select distinct departmentId
from employee
where LastName = 'Robinson'
) e2 on e1.departmentId = e2.departmentId;