Ivy Professional School
Rating
Learning/SQL Data Drill #6

Merchandising

Last updated

Analyze product returns, store-level product demand, and return-heavy products to support better merchandising decisions.

SQLIntermediateJOINGROUP BYWindow FunctionsCTE

Your Objective

Analyze product returns, store-level product demand, and return-heavy products to support better merchandising decisions.

The Setup

Your dataset contains five tables from a retail merchandising system: 1. A Products table with product_id, product_name, and category 2. A Stores table with store_id and store_name 3. An Orders table with order_id and the store_id where the order was placed 4. An Order_Items table with order_item_id, order_id, product_id, and quantity sold 5. A Returns table with return_id, order_item_id, quantity_returned, and refund_amount

Table Schema

Products
ColumnTypeKey / Notes
product_idINTPK
product_nameVARCHAR(100)
categoryVARCHAR(50)
Stores
ColumnTypeKey / Notes
store_idINTPK
store_nameVARCHAR(100)
Orders
ColumnTypeKey / Notes
order_idINTPK
store_idINTFK
Order_Items
ColumnTypeKey / Notes
order_item_idINTPK
order_idINTFK
product_idINTFK
quantityINT
Returns
ColumnTypeKey / Notes
return_idINTPK
order_item_idINTFK
quantity_returnedINT
refund_amountDECIMAL(10,2)

Sample Data

Products — sample

product_idproduct_namecategory
1Wireless EarbudsElectronics
2Smart WatchElectronics
3Denim JacketFashion
4Running ShoesFootwear
5BackpackFashion

Stores — sample

store_idstore_name
1Kolkata Store
2Delhi Store

Orders — sample

order_idstore_id
10011
10021
10032
10042

Order_Items — sample

order_item_idorder_idproduct_idquantity
11001120
21001415
31002310
41003225
51003515
61004110

Returns — sample

return_idorder_item_idquantity_returnedrefund_amount
1122,400.00
221900.00
3433,600.00
451750.00

Your Task

This drill has four tasks: 1. Return Impact — Find the return rate in each product category along with the total and average refund value. Your result table should have: category, return_rate, avg_refund_amount, total_refund_amount. 2. Product Demand by Store — In each store, which products are more in demand based on quantity sold? Find the contribution of each product to qty sold within its store. Your result table should have: store_name, product_name, total_qty, contribution. 3. Product Returns by Store — In each store, which products are returned more? Solve in the same way as Question 2. Your result table should have: store_name, product_name, total_qty, contribution. 4. Insight Question — Is there any product in a store that has both high demand and high return? Answer in 3 lines — no query needed.

Your output should look like this (partial example):

categoryreturn_rateavg_refund_amounttotal_refund_amount
Electronics8.331200.003600.00
Fashion6.25750.002250.00
…………

Hints

  • →For Q1, start from order_items (it has quantity sold). Use LEFT JOIN with returns so categories with zero returns still appear — INNER JOIN would silently drop them.
  • →For Q2, build a CTE that aggregates total_qty per (store_id, product_id). Then in the outer query, use SUM(total_qty) OVER (PARTITION BY store_name) to get the store-level total for the contribution %.
  • →For Q3, follow the same CTE structure as Q2 but replace SUM(quantity) with SUM(quantity_returned) and use INNER JOIN with returns — you only care about products that were actually returned.
Explore Enterprise Solutions