Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products by Sales Revenue

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

Your question is Top Products by Sales Revenue. 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

NovaCart wants to identify its highest-revenue products from completed sales. Write a SQL query to return the top 10 products by total sales revenue.

Requirements

  1. Use only completed orders when calculating revenue.
  2. Compute product revenue as quantity * unit_price from order line items.
  3. Return each product's product_id, product_name, and total revenue.
  4. Sort by total revenue descending, then by product_id ascending to break ties.
  5. Return only the top 10 rows.

Schema

products
ColumnTypeDescription
product_idPKINTPrimary key for the product
product_nameVARCHAR(100)Display name of the product
categoryVARCHAR(50)Product category
orders
ColumnTypeDescription
order_idPKINTPrimary key for the order
customer_idINTIdentifier for the customer
order_dateDATEDate the order was placed
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTPrimary key for the order line
order_idINTReferences orders.order_id
product_idINTReferences products.product_id
quantityINTNumber of units purchased
unit_priceDECIMAL(10,2)Unit price captured at purchase time
Tablesproductsordersorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results