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 Window Functions Ranking
00:00
5 left

SQL Window Functions Ranking

EasySQL · PostgreSQL

Problem

Career Soft Solutions Pvt. uses a product revenue report to identify leading products each month. Write a PostgreSQL query that ranks products by revenue within every category and month.

Requirements

  1. Include only orders whose status is completed.
  2. Calculate product revenue as quantity * unit_price, aggregated by calendar month, category, and product.
  3. Use a window function to assign a deterministic rank within each month and category, ordering revenue descending and product_id ascending for ties.
  4. Return only the top three products in each category for each month, including categories with fewer than three qualifying products.
  5. Return results ordered by month, category, rank, and product ID.

Schema

categories
ColumnTypeDescription
category_idPKINTPrimary key for a product category
category_nameVARCHAR(80)Display name of the category
products
ColumnTypeDescription
product_idPKINTPrimary key for a product
product_nameVARCHAR(120)Product display name
category_idINTReference to categories.category_id
orders
ColumnTypeDescription
order_idPKINTPrimary key for an order
order_dateTIMESTAMPTimestamp when the order was placed
statusVARCHAR(20)Order processing status
order_items
ColumnTypeDescription
order_item_idPKINTPrimary key for an order line item
order_idINTReference to orders.order_id
product_idINTReference to products.product_id
quantityINTNumber of units in the line item
unit_priceNUMERIC(12,2)Price per unit at purchase time
Tablescategoriesproductsordersorder_items
Interviewer

Your question is SQL Window Functions Ranking. Start with the requirements and the four 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.