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
SQL for Top 3 Products
00:00
5 left

SQL for Top 3 Products

MediumSQL · PostgreSQL

Problem

Goldman Sachs reporting needs a category-level view of product revenue for the most recent 30-day reporting window. Assume the report runs on 2025-02-15, so include orders from 2025-01-17 through 2025-02-15, inclusive.

Write a PostgreSQL query that returns the top three products by revenue within each category.

Requirements

  1. Join Goldman Sachs product, category, and order data.
  2. Calculate revenue as quantity * unit_price for orders in the 30-day window.
  3. Rank products independently within each category and return the top three, using product ID as a deterministic tie-breaker.
  4. Return results ordered by category, revenue descending, and product ID.

Schema

product_categories
ColumnTypeDescription
category_idPKINTUnique product category identifier
category_nameVARCHAR(100)Category name used in reporting
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(150)Product name
category_idINTReferences product_categories.category_id
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
product_idINTReferences products.product_id when available
order_dateDATEDate the order was placed
quantityINTNumber of units ordered
unit_priceNUMERIC(12,2)Price per unit
Tablesproduct_categoriesproductsorders
Interviewer

Your question is SQL for Top 3 Products. 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.