当前位置:网站首页>Database daily question --- day 9: salesperson

Database daily question --- day 9: salesperson

2022-06-11 21:20:00 InfoQ


One 、 Problem description

surface
: 
SalesPerson

+-----------------+---------+
| Column Name | Type |
+-----------------+---------+
| sales_id | int |
| name | varchar |
| salary | int |
| commission_rate | int |
| hire_date | date |
+-----------------+---------+
sales_id  Is the primary key column of the table .
Each row of the table shows the salesperson's name and  ID , And their wages 、 Commission rate and date of employment .

surface
: 
Company

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| com_id | int |
| name | varchar |
| city | varchar |
+-------------+---------+
com_id  Is the primary key column of the table .
Each row of the table indicates the name of the company and  ID , And the city where the company is located .

surface
: 
Orders

+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id | int |
| order_date | date |
| com_id | int |
| sales_id | int |
| amount | int |
+-------------+------+
order_id  Is the primary key column of the table .
com_id  yes  Company  In the table  com_id  The foreign key .
sales_id  It's from the salesperson table  sales_id  The foreign key .
Each row of the table contains the information of an order . This includes the company's  ID 、 Salesperson's  ID 、 Date of order and amount paid .

Write a SQL Inquire about , The report has nothing to do with the name   
“RED”
   The names of all salespeople for orders related to the company .

With  
In any order
  Return result table .

Topic link :
Salesperson

Two 、 Subject requirements

Examples

Input :
SalesPerson  surface :
+----------+------+--------+-----------------+------------+
| sales_id | name | salary | commission_rate | hire_date |
+----------+------+--------+-----------------+------------+
| 1 | John | 100000 | 6 | 4/1/2006 |
| 2 | Amy | 12000 | 5 | 5/1/2010 |
| 3 | Mark | 65000 | 12 | 12/25/2008 |
| 4 | Pam | 25000 | 25 | 1/1/2005 |
| 5 | Alex | 5000 | 10 | 2/3/2007 |
+----------+------+--------+-----------------+------------+
Company  surface :
+--------+--------+----------+
| com_id | name | city |
+--------+--------+----------+
| 1 | RED | Boston |
| 2 | ORANGE | New York |
| 3 | YELLOW | Boston |
| 4 | GREEN | Austin |
+--------+--------+----------+
Orders  surface :
+----------+------------+--------+----------+--------+
| order_id | order_date | com_id | sales_id | amount |
+----------+------------+--------+----------+--------+
| 1 | 1/1/2014 | 3 | 4 | 10000 |
| 2 | 2/1/2014 | 4 | 5 | 5000 |
| 3 | 3/1/2014 | 1 | 1 | 50000 |
| 4 | 4/1/2014 | 1 | 4 | 25000 |
+----------+------------+--------+----------+--------+
Output :
+------+
| name |
+------+
| Amy |
| Mark |
| Alex |
+------+
explain :
According to the table  orders  Order in  '3'  and  '4' , It's easy to see that there is only  'John'  and  'Pam'  Two salesmen once told the company  'RED'  Sold .
So we need output meters  salesperson  The names of all the others in .

Investigate

1. Query statement 、 Connection problem
2. It is recommended to use time 10~25min

3、 ... and 、 Problem analysis

1. Subquery

The first method is more common , An easy way to think of .

We can start at
Company  and Orders 
From the two tables, select the ones that meet the conditions of the topic
id
Number , Finally, add a step of judgment
SalesPerson
As long as the number inside is not in the sub condition statement .

2. Left connection

We go through
left join sentence
First go left to connect , The results are shown in the following figure :

| order_id | date | com_id | sales_id | amount | com_id | name | city |
|----------|----------|--------|----------|--------|--------|------|--------|
| 3 | 3/1/2014 | 1 | 1 | 50000 | 1 | RED | Boston |
| 4 | 4/1/2014 | 1 | 4 | 25000 | 1 | RED | Boston |

Actually, I see the table above , The rest of the process is really judgment
SalesPerson
Table is not
sales_id 
Did it appear in the table above .

Four 、 coded

1. Subquery

select s.name
from SalesPerson s
where s.sales_id not in
(
 select o.sales_id 
 from Company c,Orders o 
 where c.name='RED' and c.com_id=o.com_id
)

2. Left connection

select s.name
from SalesPerson s
where s.sales_id not in
(
 select o.sales_id
 from Orders o
 left join Company c on c.com_id=o.com_id
 where c.name='RED'
)

5、 ... and 、 test result

null
null
null
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112110461529.html