Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products by Monthly Share

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Products by Monthly Share. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Groww's Business Analytics team wants to compare which investment products contribute the most and least to monthly completed order value. Write a PostgreSQL query that calculates each product's share of monthly sales and returns the top five and last five products for every month.

Requirements

  1. Join completed sales to the product catalog and aggregate sales amount by calendar month and product.
  2. Calculate each product's percentage share of its month's total sales.
  3. Return both the five highest-ranked and five lowest-ranked products for each month.
  4. Include a segment label, TOP 5 or LAST 5, and use deterministic ranking by revenue and product_id.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Groww product name
categoryVARCHAR(50)Product category
sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTReferenced product identifier
sold_atTIMESTAMPTimestamp when the sale was recorded
amountNUMERIC(12,2)Monetary value of the sale
statusVARCHAR(20)Transaction processing status
Tablesproductssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results