給定 3 個表:?salesperson,?company,?orders。
輸出所有表?salesperson?中,沒有向公司 'RED' 銷售任何東西的銷售員。
解釋
輸入
表:?salesperson
+----------+------+--------+-----------------+-----------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+-----------+
| ? 1 ? ? ?| John | 100000 | ? ? 6 ? ? ? ? ? | 4/1/2006 ?|
| ? 2 ? ? ?| Amy ?| 120000 | ? ? 5 ? ? ? ? ? | 5/1/2010 ?|
| ? 3 ? ? ?| Mark | 65000 ?| ? ? 12 ? ? ? ? ?| 12/25/2008|
| ? 4 ? ? ?| Pam ?| 25000 ?| ? ? 25 ? ? ? ? ?| 1/1/2005 ?|
| ? 5 ? ? ?| Alex | 50000 ?| ? ? 10 ? ? ? ? ?| 2/3/2007 ?|
+----------+------+--------+-----------------+-----------+
表?salesperson 存儲了所有銷售員的信息。每個銷售員都有一個銷售員編號?sales_id 和他的名字?name?。
表:?company
+---------+--------+------------+
| com_id ?| ?name ?| ? ?city ? ?|
+---------+--------+------------+
| ? 1 ? ? | ?RED ? | ? Boston ? |
| ? 2 ? ? | ORANGE | ? New York |
| ? 3 ? ? | YELLOW | ? Boston ? |
| ? 4 ? ? | GREEN ?| ? Austin ? |
+---------+--------+------------+
表?company?存儲了所有公司的信息。每個公司都有一個公司編號?com_id?和它的名字 name?。
表:?orders
+----------+------------+---------+----------+--------+
| order_id | order_date | com_id ?| sales_id | amount |
+----------+------------+---------+----------+--------+
| 1 ? ? ? ?| ? 1/1/2014 | ? ?3 ? ?| ? ?4 ? ? | 100000 |
| 2 ? ? ? ?| ? 2/1/2014 | ? ?4 ? ?| ? ?5 ? ? | 5000 ? |
| 3 ? ? ? ?| ? 3/1/2014 | ? ?1 ? ?| ? ?1 ? ? | 50000 ?|
| 4 ? ? ? ?| ? 4/1/2014 | ? ?1 ? ?| ? ?4 ? ? | 25000 ?|
+----------+----------+---------+----------+--------+
表?orders?存儲了所有的銷售數據,包括銷售員編號 sales_id 和公司編號 com_id?。
輸出
+------+
| name |?
+------+
| Amy ?|?
| Mark |?
| Alex |
+------+
解釋
根據表?orders?中的訂單 '3' 和 '4' ,容易看出只有 'John' 和 'Pam' 兩個銷售員曾經向公司 'RED' 銷售過。
所以我們需要輸出表?salesperson?中所有其他人的名字。
select name
from salesperson
where sales_id not in(select sales_id from orders where com_id=(select com_id from company where name='RED'));
not in內部:把記錄里和RED公司的id有關的個人查出來即可。
然后判斷沒關系的,不在此名單中。
?