To give an alternative to a_horse_with_no_name's answer this can also be done with a left outer join
:
select s.*
from staff s
left outer join ward w
on s.staffno = w.wardmanager
where w.wardmanager is null
This works be joining on what you want but then asking for values that do not match.
As to which is quicker / more efficient it depends on your specific circumstances so you would have to test both and see.
As there is no, relevant, data in the ward
table ( all columns will be null ) there is no point selecting columns from this table. However, the benefit of a join
is that you can, if you want. Say, for instance, you removed the where
clause you could return staff that are ward manager's and staff that are not. The differentiation being that columns from ward
would always be null when they are not a manager.
Yet another method of doing this is using where exists
, but like not in
this will only be suitable if you do not want to return any columns from ward
.
select s.*
from staff s
where not exists ( select 1
from ward w
where wardmanager = s.staffno )
Once again speed and efficiency is determined by your circumstances and how many rows there are in the tables.