Problem
Table: Customer
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| customer_name | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
Each row of this table contains the information of each customer in the WebStore.
Table: Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| sale_date | date |
| order_cost | int |
| customer_id | int |
| seller_id | int |
+---------------+---------+
order_id is the column with unique values 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
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| seller_id | int |
| seller_name | varchar |
+---------------+---------+
seller_id is the column with unique values for this table.
Each row of this table contains the information of each seller.
Write a solution 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 result format is in the following example.
Examples
Example 1:
|
|
Solution
Method 1 - SQL Anti-Join and Pandas Filtering
Intuition
We want to find all sellers who did not make any sales in 2020. This means we need to find sellers whose seller_id
does not appear in any order with a sale_date
in 2020. This is a classic anti-join problem.
Approach
- Find all sellers who made at least one sale in 2020 (from the
Orders
table). - Select all sellers from the
Seller
table whoseseller_id
is not in the set of sellers with 2020 sales. - Order the result by
seller_name
ascending.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(N + M)
where N = number of sellers, M = number of orders - 🧺 Space complexity:
O(N)
for storing the result