Sellers With No Sales Problem
Description
LeetCode Problem 1607.
Table: Customer
1
2
3
4
5
6
7
8
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| customer_name | varchar |
+---------------+---------+
customer_id is the primary key for this table.
Each row of this table contains the information of each customer in the WebStore.
Table: Orders
1
2
3
4
5
6
7
8
9
10
11
12
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| sale_date | date |
| order_cost | int |
| customer_id | int |
| seller_id | int |
+---------------+---------+
order_id is the primary key for this table.
Each row of this table contains all orders made in the webstore.
sale_date is the date when the transaction was made between the customer (customer_id) and the seller (seller_id).
Table: Seller
1
2
3
4
5
6
7
8
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| seller_id | int |
| seller_name | varchar |
+---------------+---------+
seller_id is the primary key for this table.
Each row of this table contains the information of each seller.
Write an SQL query to report the names of all sellers who did not make any sales in 2020.
Return the result table ordered by seller_name in ascending order.
The query result format is in the following example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Customer table:
+--------------+---------------+
| customer_id | customer_name |
+--------------+---------------+
| 101 | Alice |
| 102 | Bob |
| 103 | Charlie |
+--------------+---------------+
Orders table:
+-------------+------------+--------------+-------------+-------------+
| order_id | sale_date | order_cost | customer_id | seller_id |
+-------------+------------+--------------+-------------+-------------+
| 1 | 2020-03-01 | 1500 | 101 | 1 |
| 2 | 2020-05-25 | 2400 | 102 | 2 |
| 3 | 2019-05-25 | 800 | 101 | 3 |
| 4 | 2020-09-13 | 1000 | 103 | 2 |
| 5 | 2019-02-11 | 700 | 101 | 2 |
+-------------+------------+--------------+-------------+-------------+
Seller table:
+-------------+-------------+
| seller_id | seller_name |
+-------------+-------------+
| 1 | Daniel |
| 2 | Elizabeth |
| 3 | Frank |
+-------------+-------------+
Result table:
+-------------+
| seller_name |
+-------------+
| Frank |
+-------------+
Daniel made 1 sale in March 2020.
Elizabeth made 2 sales in 2020 and 1 sale in 2019.
Frank made 1 sale in 2019 but no sales in 2020.
MySQL Solution
1
2
3
4
5
6
7
8
9
10
select sellInfo.seller_name as seller_name
from (
select s.seller_name as seller_name, max(o.sale_date) as latest_sale_date
from Orders o
right join Seller s
on o.seller_id = s.seller_id
group by s.seller_id
) sellInfo
where sellInfo.latest_sale_date is null or datediff('2020-01-01', sellInfo.latest_sale_date) > 0
order by seller_name
LeetCode Database - Easy
LeetCode 1407
LeetCode 1435
LeetCode 1484
LeetCode 1495
LeetCode 1511
LeetCode 1517
LeetCode 1527
LeetCode 1543
LeetCode 1565
LeetCode 1571
LeetCode 1581
LeetCode 1587
LeetCode 1607
LeetCode 1623
LeetCode 1633
LeetCode 1661
LeetCode 1667
LeetCode 1677
LeetCode 1683
LeetCode 1693
LeetCode 1729
LeetCode 1731
LeetCode 1741
LeetCode 1757
LeetCode 1777
LeetCode 1789
LeetCode 1795
LeetCode 1809
More LeetCode Database
MySQL Tutorials