Practice / window functions

Best Seller Per Store

medium

A retail chain wants each store's single best-selling product.

product_sales

Your task: return store, product, and units for the best-selling product in each store (the highest units). If two products in a store tie on units, keep the one whose product name comes first alphabetically — so every store returns exactly one row.

Assign your answer DataFrame to result. Row order doesn't matter.

product_salesinput DataFrame

Schema
columntype
storestring
productstring
unitslong
Sample rows
storeproductunits
DowntownPen40
DowntownInk55
PierPen20
PierPad35
UptownMug30
UptownCup30
Expected output shape
storeproductunits· 3 rows
Hint

Window.partitionBy("store").orderBy(F.col("units").desc(), F.col("product").asc()), then F.row_number().over(w) == 1. The product tiebreaker keeps exactly one row per store when two products sell the same number of units.

Lesson refresher

This problem builds on Window Functions (~9 min). Pop it open in a new tab if you want a quick recap.

Loading editor…
Hit Run to execute your code and see the output here.