Leaderboard Shake-Up
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.
| shop_id | shop_name | revenue |
|---|---|---|
| 3 | Maple | 1400 |
| 1 | Harbor | 2100 |
| 2 | Birch | 1390 |
| shop_id | revenue_delta |
|---|---|
| 3 | 600 |
| 2 | 0 |
| 4 | 10 |
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