List the Products Ordered in a Period LeetCode Solution

This Leetcode problem List the Products Ordered in a Period LeetCode Solution is done in SQL.

List of all LeetCode Solution

List the Products Ordered in a Period LeetCode Solution

List the Products Ordered in a Period LeetCode Solution

Problem Statement

Column NameType
product_idint
product_namevarchar
product_categoryvarchar
Table: Products

product_id is the primary key (column with unique values) for this table. This table contains data about the company’s products.

Column NameType
product_id int
order_date date
unitint
Table: Orders

This table may have duplicate rows. product_id is a foreign key (reference column) to the Products table. unit is the number of products ordered in order_date.

Write a solution to get the names of products that have at least 100 units ordered in February 2020 and their amount.

Return the result table in any order.

The result format is in the following example.

Example 1:
Input:

product_id product_name product_category
1Leetcode SolutionsBook
2Jewels of StringologyBook
3HPLaptop
4LenovoLaptop
5Leetcode KitT-shirt
Products table:
product_id order_date unit
12020-02-0560
12020-02-1070
22020-01-1830
22020-02-1180
32020-02-172
32020-02-243
42020-03-0120
42020-03-0430
42020-03-0460
52020-02-2550
52020-02-2750
52020-03-0150
Orders table:

Output:

product_name unit
Leetcode Solutions130
Leetcode Kit100

Explanation:
Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
Products with product_id = 2 is ordered in February a total of 80.
Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
Products with product_id = 4 was not ordered in February 2020.
Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.

List the Products Ordered in a Period LeetCode Solution MySQL

select 
  product_name, 
  sum(unit) as unit 
from 
  Orders as o 
  left join Products as p on o.product_id = p.product_id 
where 
  order_date between '2020-02-01' 
  and '2020-02-29' 
group by 
  o.product_id 
having 
  sum(unit) >= 100;Code language: SQL (Structured Query Language) (sql)
Scroll to Top