坚持写这个嵌套子查询,被加入 table 的步骤搞糊涂了

stuck on writing this nested subquery, confused by a step on joining table

Employee(fname, minit, lname, SSN, bdate, address, sex, salary, superssn, dno); fname,minit,lname is the employee's name; bdate is birth date; superssn is supervisor's social security #; dno is dept #
Department(dname, DNUMBER, mgrssn, mgrstartdate); mgrssn is manager ssn
Dept_locations(DNUMBER, DLOCATION); dlocation is department location
Project(Pname, PNUMBER, plocation, dnum)
Works_on(ESSN, PNO, hours); ESSN is employee ssn, pno is project number
Dependent(ESSN, DEPENDENT_NAME, sex, bdate, relationship)

我想列出不在项目 7 上工作且位于底特律的所有经理的姓氏和名字、SSN

这看起来不错:

Select e.ssn, e.lname, e.fname
from employee e, department d
where (d.mgrssn = e.ssn)

where e.ssn NOT in (
        select w.essn
        from works_on w
        where w.pno = '07'
        and p.plocation = 'Detroit'
)

我想我需要在pno = 07之前放一个pno = works_on联合声明,但是有人告诉我不需要它。所以我现在真的很困惑。

我还需要在括号中包含 where (d.mgrssn = e.ssn) 吗?

这里是带连接的长格式查询:

select e.ssn, e.lname, e.fname
from employee e
  join works_on wo on wo.ESSN = e.ssn
  join project PO on po.pnumber = wo.pno
  join dept_location dl on dl.dnumber = po.dnum
where dl.dlocation != 'Detroit'
  or po.pnumber != 7

如果您想避免大量连接,这里是带有 not in 的相同查询:

select e.ssn, e.lname, e.fname
from employee e
where e.ssn not in
(select wo.essn from works_on wo
  join dept_locations dl on dnumber = wo.pno
  where dl.dlocation = 'Detroit'
    or po.pnumber = 7)

希望对您有所帮助。