Monthly Item Sales

ASKED IN INTERVIEW
4pts
Amazon

Problem Statement

Amazon inventory managers need to understand seasonal demand shifts. To do this, they compare the sales performance of products in the current month against their performance in the same month of the previous year.

Write an SQL query to report the total quantity sold for each item in each month, alongside the total quantity sold for that same item in the same month of the preceding year.

Rules:

  • The output should include the item name, the month (1-12), the current year, the current year's total units, and the previous year's total units.
  • If an item had no sales in the same month of the previous year, the previous year's total should be 0.
  • Only include rows where there were sales in the "current" year being reported.
  • Output columns: item_name, sale_month, sale_year, current_year_units, prev_year_units.
  • Results must be sorted by item_name ASC, sale_year ASC, and sale_month ASC.
Tests your understanding of
Window Functions, Join and Date Manipulation

Input Tables

Items
item_id(INTEGER)item_name(VARCHAR)
1Kindle
2Echo Dot
3Fire TV
4Ring Cam
5Eero Router
6Blink Mini
Sales
sale_id(INTEGER)item_id(INTEGER)quantity(INTEGER)sale_date(DATE)
1011102024-01-15
1021152025-01-20
103252024-02-10
104282025-02-12
1053202025-03-05
106152024-01-25
Inventory
item_id(INTEGER)warehouse_id(INTEGER)stock_on_hand(INTEGER)
11500
21300
32100
42150
53200
63400

Expected Output

item_name(VARCHAR)sale_month(INTEGER)sale_year(INTEGER)current_year_units(INTEGER)prev_year_units(INTEGER)
Echo Dot2202450
Echo Dot2202585
Fire TV32025200
Kindle12024150
Kindle120251515

Tags

MediumASKED IN INTERVIEWWindow FunctionsJoinDate Manipulation
15-20 min
60%

Hints