|
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -None |
| 7 | +<p>Given three tables: <code>salesperson</code>, <code>company</code>, <code>orders</code>.<br /> |
| 8 | +Output all the <b>names</b> in the table <code>salesperson</code>, who didn’t have sales to company 'RED'.</p> |
| 9 | + |
| 10 | +<p><b>Example</b><br /> |
| 11 | +<b>Input</b></p> |
| 12 | + |
| 13 | +<p>Table: <code>salesperson</code></p> |
| 14 | + |
| 15 | +<pre> |
| 16 | ++----------+------+--------+-----------------+-----------+ |
| 17 | +| sales_id | name | salary | commission_rate | hire_date | |
| 18 | ++----------+------+--------+-----------------+-----------+ |
| 19 | +| 1 | John | 100000 | 6 | 4/1/2006 | |
| 20 | +| 2 | Amy | 120000 | 5 | 5/1/2010 | |
| 21 | +| 3 | Mark | 65000 | 12 | 12/25/2008| |
| 22 | +| 4 | Pam | 25000 | 25 | 1/1/2005 | |
| 23 | +| 5 | Alex | 50000 | 10 | 2/3/2007 | |
| 24 | ++----------+------+--------+-----------------+-----------+ |
| 25 | +</pre> |
| 26 | + |
| 27 | +The table <code>salesperson</code> holds the salesperson information. Every salesperson has a <b>sales_id</b> and a <b>name</b>. |
| 28 | + |
| 29 | +<p>Table: <code>company</code></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | ++---------+--------+------------+ |
| 33 | +| com_id | name | city | |
| 34 | ++---------+--------+------------+ |
| 35 | +| 1 | RED | Boston | |
| 36 | +| 2 | ORANGE | New York | |
| 37 | +| 3 | YELLOW | Boston | |
| 38 | +| 4 | GREEN | Austin | |
| 39 | ++---------+--------+------------+ |
| 40 | +</pre> |
| 41 | + |
| 42 | +The table <code>company</code> holds the company information. Every company has a <b>com_id</b> and a <b>name</b>. |
| 43 | + |
| 44 | +<p>Table: <code>orders</code></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | ++----------+------------+---------+----------+--------+ |
| 48 | +| order_id | order_date | com_id | sales_id | amount | |
| 49 | ++----------+------------+---------+----------+--------+ |
| 50 | +| 1 | 1/1/2014 | 3 | 4 | 100000 | |
| 51 | +| 2 | 2/1/2014 | 4 | 5 | 5000 | |
| 52 | +| 3 | 3/1/2014 | 1 | 1 | 50000 | |
| 53 | +| 4 | 4/1/2014 | 1 | 4 | 25000 | |
| 54 | ++----------+----------+---------+----------+--------+ |
| 55 | +</pre> |
| 56 | + |
| 57 | +The table <code>orders</code> holds the sales record information, salesperson and customer company are represented by <b>sales_id</b> and <b>com_id</b>. |
| 58 | + |
| 59 | +<p><b>output</b></p> |
| 60 | + |
| 61 | +<pre> |
| 62 | ++------+ |
| 63 | +| name | |
| 64 | ++------+ |
| 65 | +| Amy | |
| 66 | +| Mark | |
| 67 | +| Alex | |
| 68 | ++------+ |
| 69 | +</pre> |
| 70 | + |
| 71 | +<p><b>Explanation</b></p> |
| 72 | + |
| 73 | +<p>According to order '3' and '4' in table <code>orders</code>, it is easy to tell only salesperson 'John' and 'Pam' have sales to company 'RED',<br /> |
| 74 | +so we need to output all the other <b>names</b> in the table <code>salesperson</code>.</p> |
8 | 75 |
|
9 | 76 | ## Solutions
|
10 | 77 |
|
11 | 78 | <!-- tabs:start -->
|
12 | 79 |
|
13 | 80 | ### **SQL**
|
14 | 81 |
|
15 |
| -``` |
| 82 | +```sql |
| 83 | +# Write your MySQL query statement below |
16 | 84 | SELECT name
|
17 | 85 | FROM salesperson
|
18 |
| -WHERE sales_id NOT IN |
19 |
| - (SELECT sales_id |
20 |
| - FROM orders |
21 |
| - WHERE com_id = |
22 |
| - (SELECT com_id |
23 |
| - FROM company |
24 |
| - WHERE name = 'RED')) |
| 86 | +WHERE sales_id |
| 87 | +NOT IN ( |
| 88 | + SELECT s.sales_id FROM orders o |
| 89 | + INNER JOIN salesperson s ON o.sales_id = s.sales_id |
| 90 | + INNER JOIN company c ON o.com_id = c.com_id |
| 91 | + WHERE c.name = 'RED' |
| 92 | +); |
25 | 93 | ```
|
26 | 94 |
|
27 | 95 | <!-- tabs:end -->
|
0 commit comments