Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Top Categories by GMV Concentration

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

Your question is SQL: Top Categories by GMV Concentration. 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

Business Context

Movate's analytics team wants to identify product categories whose annual GMV is highly concentrated in a short seasonal period.

Task

Write a PostgreSQL query for calendar year 2024 that returns the top five product categories where at least 60% of annual GMV occurs in any two consecutive calendar months.

Requirements

  1. Include all twelve months for every category, treating months without completed orders as zero GMV.
  2. Exclude orders outside 2024 and orders whose order_status is not completed.
  3. Calculate annual GMV and every two-month consecutive GMV total using window functions.
  4. Keep categories where the largest two-month total is at least 60% of annual GMV.
  5. Return the top five qualifying categories ranked by annual GMV descending, including the first month of the peak pair and its concentration percentage.

Schema

product_categories
ColumnTypeDescription
category_idPKINTUnique product category identifier
category_nameVARCHAR(100)Product category name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
category_idINTProduct category associated with the order
order_dateDATEDate on which the order was placed
gmvNUMERIC(12,2)Gross merchandise value for the order
order_statusVARCHAR(20)Order processing status
Tablesproduct_categoriesorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results