如何归还没有经理的分支机构? (SQL)

时间:2017-01-09 18:44:19

标签: sql postgresql

我正在尝试返回没有经理的分支机构。

Here are the tables

这就是我的尝试:

SELECT branchno, city FROM branch
WHERE branchno IN
(SELECT branchno FROM STAFF
WHERE position <> 'Manager');

它返回:

 branchno |   city
----------+----------
 B005     | London
 B007     | Aberdeen
 B003     | Glasgow
 (3 rows)

2 个答案:

答案 0 :(得分:1)

您可以使用not in

轻松完成此操作
select *
from branch
where branchno not in (
  select branchno from staff
  where position = 'Manager'
);

答案 1 :(得分:1)

有很多方法,您可以使用下面的WHERE NOT EXISTS。顺便说一下,您发布的查询也应该为您提供所需的结果(根据您的帖子标题)。你还在寻找什么?

SELECT b.branchno, b.city 
FROM branch b
WHERE NOT EXISTS 
(SELECT 1 FROM STAFF WHERE branchno = b.branchno
AND position = 'Manager');