Sales Analysis III Problem
Description
LeetCode Problem 1084.
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
product_id is the primary key of this table.
Table: Sales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
+------ ------+---------+
This table has no primary key, it can have repeated rows.
product_id is a foreign key to Product table.
Write an SQL query that reports the products that were only sold in spring 2019. That is, between 2019-01-01 and 2019-03-31 inclusive.
The query result format is in the following example:
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
Result table:
+-------------+--------------+
| product_id | product_name |
+-------------+--------------+
| 1 | S8 |
+-------------+--------------+
The product with id 1 was only sold in spring 2019 while the other two were sold after.
Solution
Approach: GROUP BY with MIN and MAX Dates
"Only sold in spring 2019" means two things: the product was sold at least once, and every one of its sales falls between 2019-01-01 and 2019-03-31, both days included.
- Join
ProducttoSales. The inner join drops products that were never sold (Pixel in the test data), which must not be reported. GROUP BYthe product.- All of a product's sales lie in the range exactly when its earliest sale is on or after 2019-01-01 and its latest sale is on or before 2019-03-31.
HAVING MIN(s.sale_date) >= '2019-01-01' AND MAX(s.sale_date) <= '2019-03-31'checks that.
Checking only WHERE sale_date BETWEEN ... is not enough: G4 has a sale in February, but also one in June, so it must be excluded. The range is inclusive, so a sale on 2019-03-31 still counts as spring.
The Pandas version takes the min and max sale_date per product, keeps the products whose whole range is inside the dates, and looks up their names in product.
SELECT p.product_id,
p.product_name
FROM Product AS p
JOIN Sales AS s
ON s.product_id = p.product_id
GROUP BY p.product_id, p.product_name
-- Every sale of the product is inside the range when the earliest and the latest one are.
HAVING MIN(s.sale_date) >= '2019-01-01'
AND MAX(s.sale_date) <= '2019-03-31';