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
Advanced SQL With Windows and Joins
00:00
5 left

Advanced SQL With Windows and Joins

HardSQL · PostgreSQL

Problem

Can you write a complex SQL query involving window functions and multiple joins to identify top-performing product categories?

Use the provided order, product, category, line-item, and return data. Consider completed orders from the current and prior comparison periods, subtract returned quantities, and return the three highest-ranked categories, including ties.

Output

  1. One row per qualifying category with category_name, sales_rank, current_net_sales, prior_net_sales, sales_growth_pct, latest_month, latest_month_sales, previous_observed_month_sales, and month_over_month_change
  2. Include categories with current-period sales only, ordered by rank ascending and category name ascending

Schema

categories
ColumnTypeDescription
category_idPKINTProduct category identifier
category_nameVARCHAR(100)Product category name
department_nameVARCHAR(100)Merchandising department
products
ColumnTypeDescription
product_idPKINTProduct identifier
product_nameVARCHAR(150)Product name
category_idINTProduct category identifier
orders
ColumnTypeDescription
order_idPKINTOrder identifier
order_dateDATEOrder date
order_statusVARCHAR(30)Order lifecycle status
sales_channelVARCHAR(30)Sales channel
order_items
ColumnTypeDescription
line_item_idPKINTOrder line identifier
order_idINTParent order identifier
product_idINTPurchased product identifier
quantityINTUnits purchased
unit_priceNUMERIC(10,2)Price per unit
returns
ColumnTypeDescription
return_idPKINTReturn identifier
line_item_idINTReturned order line identifier
return_quantityINTUnits returned
return_reasonVARCHAR(100)Reason for return
Tablescategoriesproductsordersorder_itemsreturns
Interviewer

Your question is Advanced SQL With Windows and Joins. Start with the requirements and the five 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.