Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top Products SQL by Category
00:00
5 left

Top Products SQL by Category

MediumSQL · PostgreSQL

Problem

HCLTech's retail analytics team needs a quarterly product leaderboard for executive reporting. Write a PostgreSQL query that evaluates product sales during the previous completed calendar quarter relative to the current date.

Calculate total units sold and total revenue for each product, associate every product with its category, and assign a position within that category. Revenue is calculated as quantity * unit_price for each order item.

Output

  1. One row per qualifying product, with columns category_name, product_name, total_units_sold, total_revenue, and revenue_rank.
  2. Include only products with sales in the previous completed calendar quarter.
  3. Return no more than three products per category.
  4. Sort by category name alphabetically, then leaderboard position ascending.
  5. Within each category, order by total revenue descending, total units sold descending, and product name ascending to resolve ties.

Schema

categories
ColumnTypeDescription
category_idPKINTPrimary key for the product category
category_nameVARCHAR(100)Display name of the category
products
ColumnTypeDescription
product_idPKINTPrimary key for the product
category_idINTReferences categories.category_id
product_nameVARCHAR(150)Display name of the product
order_items
ColumnTypeDescription
order_item_idPKINTPrimary key for the order line
product_idINTReferences products.product_id
order_dateDATEDate on which the item was sold
quantityINTNumber of units sold
unit_priceNUMERIC(10,2)Sale price per unit
Tablescategoriesproductsorder_items
Interviewer

Your question is Top Products SQL by Category. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.