SELECT Surname
FROM Worker
WHERE NOT EXISTS
(
SELECT *
FROM Project, Works_at
WHERE NOT EXISTS
(
SELECT *
FROM Works_at
WHERE Works_at.WorkerId =Worker.Id
AND Works_at.PNum = Project.PNumber
)
);
or
SELECT Surname
FROM Worker
WHERE NOT EXISTS (
SELECT *
FROM Works_at
WHERE Id = WorkerId
);
When I run the queries, It says that there is a syntax error near “SELECT”
What can go wrong?
First one selects the workers works in all Projects.
And the second one selects the worker doesn’t work in any Project.
Thanks in advance
Tables are
Worker = {Name, Surname, Id, Salary, DptNum}
Department = {DptName, DptNumber}
Works_at = {WorkerId, PNum, Hours}
Project={PName, PNumber, DptNum} and
Works_at(WorkerId) → Worker(Id)
Works_at(PNum) → Projekt(PNumber)
Project(DptNum) → Department(DptNumber)
Worker(DptNum) → Department(DptNumber)
NOT IN subselects are terrible for performance. You are better to hit with a LEFT-JOIN and testing for NULL (ie: doesn’t have a match)
This should give you a list of people who are Not associated with a project. That is based on there being a possibility of a Works At location, but no project being assigned. If there would be no record in the works at table, then it would even be simpler with