List the Products Ordered in a Period - Problem
List the Products Ordered in a Period

You are working as a data analyst for an e-commerce company and need to identify popular products during a specific time period. Your task is to find all products that had significant sales volume (at least 100 units) during February 2020.

You have access to two database tables:

Products table contains product information:
product_id: Unique identifier for each product
product_name: Name of the product
product_category: Category the product belongs to

Orders table contains order history:
product_id: References the product that was ordered
order_date: Date when the order was placed
unit: Number of units ordered

Goal: Return the names of products that had at least 100 units ordered in February 2020, along with their total units ordered during that period.

Input & Output

example_1.sql — Basic Example
$ Input: Products table: +------------+--------------+------------------+ | product_id | product_name | product_category | +------------+--------------+------------------+ | 1 | Leetcode Solutions| Book | | 2 | Jewels of Stringology| Book | | 3 | HP | Laptop | +------------+--------------+------------------+ Orders table: +------------+------------+---------+ | product_id | order_date | unit | +------------+------------+---------+ | 1 | 2020-02-05 | 60 | | 1 | 2020-02-10 | 70 | | 2 | 2020-01-18 | 30 | | 2 | 2020-02-11 | 80 | | 3 | 2020-02-17 | 2 | | 3 | 2020-02-24 | 3 | +------------+------------+---------+
Output: +--------------+---------+ | product_name | unit | +--------------+---------+ | Leetcode Solutions | 130 | +--------------+---------+
💡 Note: Only 'Leetcode Solutions' had at least 100 units ordered in February 2020 (60 + 70 = 130 units). 'Jewels of Stringology' had only 80 units in February, and 'HP' had only 5 units total.
example_2.sql — Multiple Products
$ Input: Products table: +------------+--------------+------------------+ | product_id | product_name | product_category | +------------+--------------+------------------+ | 1 | Gaming Mouse | Electronics | | 2 | Webcam | Electronics | | 3 | Notebook | Stationery | +------------+--------------+------------------+ Orders table: +------------+------------+---------+ | product_id | order_date | unit | +------------+------------+---------+ | 1 | 2020-02-01 | 50 | | 1 | 2020-02-15 | 60 | | 2 | 2020-02-10 | 120 | | 3 | 2020-02-20 | 40 | +------------+------------+---------+
Output: +--------------+---------+ | product_name | unit | +--------------+---------+ | Gaming Mouse | 110 | | Webcam | 120 | +--------------+---------+
💡 Note: Both 'Gaming Mouse' (50 + 60 = 110) and 'Webcam' (120) had at least 100 units ordered in February 2020. 'Notebook' only had 40 units.
example_3.sql — Edge Case - No Qualifying Products
$ Input: Products table: +------------+--------------+------------------+ | product_id | product_name | product_category | +------------+--------------+------------------+ | 1 | Phone Case | Electronics | | 2 | Charger | Electronics | +------------+--------------+------------------+ Orders table: +------------+------------+---------+ | product_id | order_date | unit | +------------+------------+---------+ | 1 | 2020-02-05 | 30 | | 1 | 2020-02-15 | 40 | | 2 | 2020-02-10 | 50 | +------------+------------+---------+
Output: +--------------+---------+ | product_name | unit | +--------------+---------+ (empty result set)
💡 Note: No products had at least 100 units ordered in February 2020. 'Phone Case' had 70 total units and 'Charger' had 50 units, both below the 100-unit threshold.

Constraints

  • 1 ≤ Products table rows ≤ 1000
  • 1 ≤ Orders table rows ≤ 10000
  • product_id is unique in Products table
  • order_date is in format 'YYYY-MM-DD'
  • unit ≥ 1 for all orders
  • February 2020 has 29 days (leap year)

Visualization

Tap to expand
Products1 | Laptop2 | Mouse3 | KeyboardOrders1|Feb-5|501|Feb-15|602|Feb-10|203|Feb-20|1501|Jan-5|200JOIN & FILTERproduct_id match+ Feb 2020 filter❌ Jan orders outGROUP BYproduct_idSUM(units)Laptop: 110HAVINGunit >= 100✅ Laptop: 110❌ Mouse: 20SQL Query Execution FlowSELECT p.product_name, SUM(o.unit) as unitFROM Products p INNER JOIN Orders o ON p.product_id = o.product_idWHERE o.order_date BETWEEN '2020-02-01' AND '2020-02-29'GROUP BY p.product_id HAVING SUM(o.unit) >= 100
Understanding the Visualization
1
JOIN Tables
Combine Products and Orders tables using product_id as the key
2
Filter Dates
Keep only orders from February 2020 using WHERE clause
3
Group Products
GROUP BY product_id to aggregate all orders for each product
4
Sum Units
Use SUM(unit) to calculate total units ordered per product
5
Filter Results
HAVING clause filters products with at least 100 total units
Key Takeaway
🎯 Key Insight: SQL's JOIN, GROUP BY, and HAVING clauses work together efficiently to filter, aggregate, and select data in a single optimized query execution.
Asked in
Amazon 45 Meta 38 Google 32 Microsoft 28
42.0K Views
Medium-High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen