First compute each card member's total spend (refunds are negative amounts and reduce the total). Then, per tier, find the 90th percentile of member totals using the nearest-rank method, WITHOUT percentile functions: sort totals ascending and take the value at position CEIL(0.9 * n), where n is the number of members in the tier. Return tier, members (n), p90_spend (ROUND 2) and members_at_or_above (members whose total >= p90_spend). Order by tier.
| Column | Type |
|---|---|
| txn_id | INTEGER |
| member_id | TEXT |
| tier | TEXT |
| amount | REAL |
| txn_id | member_id | tier | amount |
|---|---|---|---|
| 1 | m1 | Gold | 100 |
| 2 | m2 | Gold | 200 |
| 3 | m3 | Gold | 300 |
| 4 | m4 | Gold | 400 |
| 5 | m5 | Gold | 500 |
| 6 | p1 | Platinum | 600 |
| 7 | p1 | Platinum | 400 |
| 8 | p2 | Platinum | 3000 |
| tier | members | p90_spend | members_at_or_above |
|---|---|---|---|
| Gold | 5 | 500 | 1 |
| Platinum | 3 | 3000 | 1 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Structuring: Near-Threshold Deposits in 7 Days · Month-over-Month Credit Spikes ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.