Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top 5 by Sales Volume

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

Your question is SQL Top 5 by Sales Volume. 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

Octane wants to identify which products generate the highest sales volume. Write a PostgreSQL query that ranks products by the total number of units sold in completed orders.

Requirements

  1. Include only line items belonging to orders with status = 'completed'.
  2. Aggregate quantity by product, preserving products with no qualifying sales.
  3. Return the top 5 products, ordered by units sold descending, with product_id ascending as the tie-breaker.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product display name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
statusVARCHAR(20)Current order status
order_items
ColumnTypeDescription
order_idINTOrder containing the item
product_idINTProduct included in the order line
quantityINTNumber of units on the order line
Tablesproductsordersorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results