Skip to content

Commit 9abcbd5

Browse files
committed
feat: add sql solution to leetcode problem: No.0607
See https://leetcode-cn.com/problems/game-play-analysis-i
1 parent 00d1a49 commit 9abcbd5

File tree

3 files changed

+159
-25
lines changed

3 files changed

+159
-25
lines changed

solution/0600-0699/0607.Sales Person/README.md

+74-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,71 @@
66

77
<!-- 这里写题目描述 -->
88

9-
None
9+
<p>给定 3 个表:&nbsp;<code>salesperson</code>,&nbsp;<code>company</code>,&nbsp;<code>orders</code>。<br>
10+
输出所有表&nbsp;<code>salesperson</code>&nbsp;中,没有向公司 &#39;RED&#39; 销售任何东西的销售员。</p>
11+
12+
<p><strong>示例:</strong><br>
13+
<strong>输入</strong></p>
14+
15+
<p>表:&nbsp;<code>salesperson</code></p>
16+
17+
<pre>+----------+------+--------+-----------------+-----------+
18+
| sales_id | name | salary | commission_rate | hire_date |
19+
+----------+------+--------+-----------------+-----------+
20+
| 1 | John | 100000 | 6 | 4/1/2006 |
21+
| 2 | Amy | 120000 | 5 | 5/1/2010 |
22+
| 3 | Mark | 65000 | 12 | 12/25/2008|
23+
| 4 | Pam | 25000 | 25 | 1/1/2005 |
24+
| 5 | Alex | 50000 | 10 | 2/3/2007 |
25+
+----------+------+--------+-----------------+-----------+
26+
</pre>
27+
28+
<p>表&nbsp;<code>salesperson</code> 存储了所有销售员的信息。每个销售员都有一个销售员编号&nbsp;<strong>sales_id</strong> 和他的名字&nbsp;<strong>name&nbsp;</strong>。</p>
29+
30+
<p>表:&nbsp;<code>company</code></p>
31+
32+
<pre>+---------+--------+------------+
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+
<p>表&nbsp;<code>company</code>&nbsp;存储了所有公司的信息。每个公司都有一个公司编号&nbsp;<strong>com_id</strong>&nbsp;和它的名字 <strong>name</strong>&nbsp;。</p>
43+
44+
<p>表:&nbsp;<code>orders</code></p>
45+
46+
<pre>+----------+------------+---------+----------+--------+
47+
| order_id | order_date | com_id | sales_id | amount |
48+
+----------+------------+---------+----------+--------+
49+
| 1 | 1/1/2014 | 3 | 4 | 100000 |
50+
| 2 | 2/1/2014 | 4 | 5 | 5000 |
51+
| 3 | 3/1/2014 | 1 | 1 | 50000 |
52+
| 4 | 4/1/2014 | 1 | 4 | 25000 |
53+
+----------+----------+---------+----------+--------+
54+
</pre>
55+
56+
<p>表&nbsp;<code>orders</code>&nbsp;存储了所有的销售数据,包括销售员编号 <strong>sales_id </strong>和公司编号 <strong>com_id</strong>&nbsp;。</p>
57+
58+
<p><strong>输出</strong></p>
59+
60+
<pre>+------+
61+
| name |
62+
+------+
63+
| Amy |
64+
| Mark |
65+
| Alex |
66+
+------+
67+
</pre>
68+
69+
<p><strong>解释</strong></p>
70+
71+
<p>根据表&nbsp;<code>orders</code>&nbsp;中的订单 &#39;3&#39;&#39;4&#39; ,容易看出只有 &#39;John&#39;&#39;Pam&#39; 两个销售员曾经向公司 &#39;RED&#39; 销售过。</p>
72+
73+
<p>所以我们需要输出表&nbsp;<code>salesperson</code>&nbsp;中所有其他人的名字。</p>
1074

1175
## 解法
1276

@@ -16,16 +80,17 @@ None
1680

1781
### **SQL**
1882

19-
```
83+
```sql
84+
# Write your MySQL query statement below
2085
SELECT name
2186
FROM salesperson
22-
WHERE sales_id NOT IN
23-
(SELECT sales_id
24-
FROM orders
25-
WHERE com_id =
26-
(SELECT com_id
27-
FROM company
28-
WHERE name = 'RED'))
87+
WHERE sales_id
88+
NOT IN (
89+
SELECT s.sales_id FROM orders o
90+
INNER JOIN salesperson s ON o.sales_id = s.sales_id
91+
INNER JOIN company c ON o.com_id = c.com_id
92+
WHERE c.name = 'RED'
93+
);
2994
```
3095

3196
<!-- tabs:end -->

solution/0600-0699/0607.Sales Person/README_EN.md

+77-9
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,92 @@
44

55
## Description
66

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&rsquo;t have sales to company &#39;RED&#39;.</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 &#39;3&#39; and &#39;4&#39; in table <code>orders</code>, it is easy to tell only salesperson &#39;John&#39; and &#39;Pam&#39; have sales to company &#39;RED&#39;,<br />
74+
so we need to output all the other <b>names</b> in the table <code>salesperson</code>.</p>
875

976
## Solutions
1077

1178
<!-- tabs:start -->
1279

1380
### **SQL**
1481

15-
```
82+
```sql
83+
# Write your MySQL query statement below
1684
SELECT name
1785
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+
);
2593
```
2694

2795
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
# Write your MySQL query statement below
12
SELECT name
23
FROM salesperson
3-
WHERE sales_id NOT IN
4-
(SELECT sales_id
5-
FROM orders
6-
WHERE com_id =
7-
(SELECT com_id
8-
FROM company
9-
WHERE name = 'RED'))
4+
WHERE sales_id
5+
NOT IN (
6+
SELECT s.sales_id FROM orders o
7+
INNER JOIN salesperson s ON o.sales_id = s.sales_id
8+
INNER JOIN company c ON o.com_id = c.com_id
9+
WHERE c.name = 'RED'
10+
);

0 commit comments

Comments
 (0)