Skip to main content
2 of 3
Fixed logic error, removed subquery
user avatar
user avatar

This can be done directly, without a CTE or subquery:

This will return which Employees have worked on a Project with a John Smith1, but who are not a John Smith:

SELECT DISTINCT
  Coworker.EmpNum
 ,Coworker.Fname
 ,Coworker.Lname
FROM
  t1 Emp
INNER JOIN
  t2 EmpProject
    ON EmpProject.EmpNum = Emp.EmpNum
INNER JOIN
  t2 CoworkerProject
    ON CoworkerProject.ProjNum = EmpProject.ProjNum
INNER JOIN
  t1 Coworker
    ON Coworker.EmpNum = CoworkerProject.EmpNum
        AND Coworker.EmpNum <> Emp.EmpNum
WHERE
  Emp.Fname = 'John'
    AND Emp.Lname = 'Smith'

1 Obviously there could be multiple John Smiths so it might be better to select a particular John Smith's EmpNum directly before searching, but if you're not concerned with which John Smith the above will suffice.

user212533