Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5 Products Per Month

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

Your question is Top 5 Products Per Month. 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

CGI's retail reporting team needs a monthly view of its best-selling products. Write a PostgreSQL query that ranks products by units sold from completed orders.

Requirements

  1. Join orders, order_items, and products, excluding canceled or unknown-status orders.
  2. Aggregate item quantities by calendar month and product.
  3. Return the five highest-selling products for each month, ordered by units sold descending and product ID ascending to break ties.

Schema

orders
ColumnTypeDescription
order_idPKINTEGERUnique order identifier
order_dateDATEDate on which the order was placed
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTEGERUnique order line identifier
order_idINTEGERReferences orders.order_id
product_idINTEGERReferences products.product_id
quantityINTEGERNumber of units on the order line
products
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(100)Display name of the product
categoryVARCHAR(50)Product category
Tablesordersorder_itemsproducts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results