Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.