AnalystPath

Leaderboard Shake-Up

SQLMediumMid level~15 min

Problem

A chain ranks its cafes on a leaderboard. `coffee_shops` has columns `(shop_id, shop_name, revenue)` (`shop_id` is the primary key), and a month of corrections is staged in `revenue_adjustments` with columns `(shop_id, revenue_delta)` (`shop_id` is the primary key, one row per shop).

A shop's rank is by `revenue` descending, breaking ties by `shop_name` ascending. After adding each shop's `revenue_delta` to its `revenue`, the ranking is recomputed the same way. Report `shop_id`, `shop_name`, and `rank_shift` = old rank minus new rank (positive means the shop climbed, negative means it fell). Row order does not matter.

Tables

Example rows — the live problem includes the full dataset.

coffee_shops
shop_idshop_namerevenue
3Maple1400
1Harbor2100
2Birch1390
revenue_adjustments
shop_idrevenue_delta
3600
20
410

Expected output

Your answer should return 4 rows with the columns shop_id, shop_name, rank_shift.

Starter code (SQL)

SELECT *
FROM coffee_shops;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions