Top Percentile Return Rates by Region
Problem
Table `Returns` holds the return rate of each product line within a sales region: `line_id`, `region`, and `return_rate` (a decimal between 0 and 1, higher is worse). The quality team wants the worst offenders within each region. For every region, find the product lines whose return rate sits in the top 5 percentile of that region, where 'top 5 percentile' means a percent-rank of at most 0.05 when the lines are ranked from highest return rate to lowest within the region.
Return `line_id`, `region`, and `return_rate`, ordered by `region` ascending, then `return_rate` descending, then `line_id` ascending.
Tables
Example rows — the live problem includes the full dataset.
| line_id | region | return_rate |
|---|---|---|
| 1 | Andes | 0.92 |
| 2 | Andes | 0.68 |
| 3 | Andes | 0.17 |
Expected output
Your answer should return 4 rows with the columns line_id, region, return_rate.
Starter code (SQL)
SELECT *
FROM Returns;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