Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top-Performing Segments Query

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

Your question is Top-Performing Segments Query. Start with the requirements and the three 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

Volkswagen Group Italia needs a quarterly view of its strongest product segments across the latest three quarters of available sales data. Write a PostgreSQL query that ranks segments by net revenue and returns the top two segments for each quarter.

Requirements

  1. Determine the latest quarter from the available sale_date values and include that quarter plus the two preceding quarters.
  2. Join sales to products and product segments, then aggregate net revenue and units by quarter and segment.
  3. Rank segments within each quarter by total revenue in descending order and return only ranks 1 and 2.
  4. Return results ordered chronologically by quarter and then by revenue rank.

Schema

product_segments
ColumnTypeDescription
segment_idPKINTUnique product segment identifier
segment_nameVARCHAR(80)Commercial product segment name
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(120)Vehicle or product name
segment_idINTReferenced product segment
sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTReferenced product
sale_dateDATEDate on which the sale was recorded
unitsINTNumber of units sold
net_revenueNUMERIC(12,2)Net sales revenue in euros
Tablessalesproductsproduct_segments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results