Dataford
Interview Guides
Upgrade
All questions/SQL & Data Manipulation/Calculate Monthly Sales Growth by Product Category

Calculate Monthly Sales Growth by Product Category

Medium
SQL & Data Manipulation
Asked at 29 companies29JoinsAggregations
Also asked at
AAppfolioIncedoProcter & GambleChimeToys "R" Us

Problem

Task

A retail company needs to analyze its sales performance. Write a SQL query to calculate the month-over-month sales growth for each product category.

Requirements

  1. Join the sales and products tables by product_id.
  2. Calculate total sales per category for each month.
  3. Determine the month-over-month growth percentage for each category.
  4. Order the results by category and month.

Schema

sales (id, product_id, sale_date, amount) products (id, category)

idproduct_idsale_dateamount
112024-01-15200
222024-01-20150
312024-02-10300
422024-02-15200
532024-02-20250
idcategory
1Electronics
2Clothing
3Accessories

Expected Output

categorymonthtotal_salesprevious_month_salesgrowth_percentage
Electronics2024-01200NULLNULL
Electronics2024-0230020050.00
Clothing2024-01150NULLNULL
Clothing2024-0220015033.33
Accessories2024-02250NULLNULL

Schema

sales
ColumnTypeDescription
idPKINTPrimary key
product_idINTForeign key referencing products
sale_dateDATEDate of the sale
amountDECIMALSale amount
products
ColumnTypeDescription
idPKINTPrimary key
categoryVARCHAR(255)Product category

Sample Data

Example 1
Input{"sales":[[1,1,"2024-01-15",200],[2,2,"2024-01-20",150],[3,1,"2024-02-10",300],[4,2,"2024-02-15",200],[5,3,"2024-02-20",250]],"products":[[1,"Electronics"],[2,"Clothing"],[3,"Accessories"]]}Output[["Accessories","2024-02","250","null","null"],["Clothing","2024-01","150","null","null"],["Clothing","2024-02","200","150","33.33"],["Electronics","2024-01","200","null","null"],["Electronics","2024-02","300","200","50.00"]]

Problem

Task

A retail company needs to analyze its sales performance. Write a SQL query to calculate the month-over-month sales growth for each product category.

Requirements

  1. Join the sales and products tables by product_id.
  2. Calculate total sales per category for each month.
  3. Determine the month-over-month growth percentage for each category.
  4. Order the results by category and month.

Schema

sales (id, product_id, sale_date, amount) products (id, category)

idproduct_idsale_dateamount
112024-01-15200
222024-01-20150
312024-02-10300
422024-02-15200
532024-02-20250
idcategory
1Electronics
2Clothing
3Accessories

Expected Output

categorymonthtotal_salesprevious_month_salesgrowth_percentage
Electronics2024-01200NULLNULL
Electronics2024-0230020050.00
Clothing2024-01150NULLNULL
Clothing2024-0220015033.33
Accessories2024-02250NULLNULL

Schema

sales
ColumnTypeDescription
idPKINTPrimary key
product_idINTForeign key referencing products
sale_dateDATEDate of the sale
amountDECIMALSale amount
products
ColumnTypeDescription
idPKINTPrimary key
categoryVARCHAR(255)Product category

Sample Data

Example 1
Input{"sales":[[1,1,"2024-01-15",200],[2,2,"2024-01-20",150],[3,1,"2024-02-10",300],[4,2,"2024-02-15",200],[5,3,"2024-02-20",250]],"products":[[1,"Electronics"],[2,"Clothing"],[3,"Accessories"]]}Output[["Accessories","2024-02","250","null","null"],["Clothing","2024-01","150","null","null"],["Clothing","2024-02","200","150","33.33"],["Electronics","2024-01","200","null","null"],["Electronics","2024-02","300","200","50.00"]]
Practice SQL
PostgreSQL
Open on desktop for the full SQL editor with syntax highlighting and autocomplete.
Up next
HD SupplyMonthly Sales Trends by CategoryMediumWalgreensMonthly Sales Aggregation by Product CategoryMediumASI (Advertising Specialty Institute)Analyze Monthly Revenue Trends by Product CategoryMedium
Next question