Third Highest Spender
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L4
- Asked in
- Amazon
Problem
Find the user with the third highest total spend across all transactions. If there is a tie, include all users sharing that rank. Show the user ID and total spend.
Bronze medal in spending.
Tables
transactions
Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date TEXT
| transaction_id | user_id | product_id | quantity | total_amount | transaction_date |
|---|---|---|---|---|---|
| 1067 | 197 | 1001 | 2 | 23.46 | 2026-02-02 |
| 1134 | 294 | 1050 | 3 | 36.93 | 2026-03-03 |
| 1201 | 391 | 1099 | 4 | 50.4 | 2026-04-04 |
| 1268 | 488 | 1148 | 5 | 63.87 | 2026-05-05 |
| 1335 | 585 | 1197 | 0 | 0 | 2026-06-06 |
| 1402 | 682 | 1246 | 2 | 90.81 | 2026-07-07 |
| 1469 | 779 | 1295 | 3 | 104.28 | 2026-08-08 |
| 1536 | 876 | 1344 | 4 | 117.75 | 2026-09-09 |
| 1603 | 973 | 1393 | 5 | -45 | 2026-01-10 |
| 1670 | 1070 | 1442 | 1 | 144.69 | 2026-02-11 |
customers
Columns: customer_id INTEGER, first_name TEXT, last_name TEXT, country TEXT
| customer_id | first_name | last_name | country |
|---|---|---|---|
| 40089 | Priya | Gupta | India |
| 40178 | Luca | Rossi | Italy |
| 40267 | Ava | Nguyen | Vietnam |
| 40356 | Ethan | Johnson | Spain |
| 40445 | Maria | Garcia | China |
| 40534 | Chen | Wei | Russia |
| 40623 | Sofia | Petrova | Ireland |
| 40712 | Liam | O'Connor | Mexico |
| 40801 | Isabella | Martinez | Canada |
| 40890 | Noah | Brown | France |
Expected output
| user_id | total_spend |
|---|---|
| 876 | 10134.6 |
Worked solution and explanation
Why this problem exists in real interviews
This is the classic Nth-highest value problem. Interviewers use it to test whether you know how to rank aggregated values with a window function and extract a specific position.
Break down the requirements
Step 1: Aggregate spend per entity
SUM(total_amount) grouped by the spender produces per-entity totals.
Step 2: Rank by total spend
DENSE_RANK() OVER (ORDER BY total_spend DESC) assigns positions. DENSE_RANK ensures tied values share the same rank.
Step 3: Filter to rank 3
Wrap in a subquery and filter WHERE rnk = 3 to extract the third-highest spender.
The solution
SELECT user_id, total_spend
FROM (
SELECT
user_id,
SUM(total_amount) AS total_spend,
DENSE_RANK() OVER (ORDER BY SUM(total_amount) DESC) AS rnk
FROM transactions
GROUP BY user_id
) ranked
WHERE rnk = 3Common follow-up questions
- What if multiple users tie for 3rd place? (Tests understanding that `DENSE_RANK` returns all tied rows at position 3.)
- How would you find the Nth highest for each category? (Tests adding PARTITION BY category to the window function.)
- Could you solve this without a window function? (Tests correlated subquery: COUNT(DISTINCT higher values) = 2.)
- What if there are fewer than 3 distinct spend values? (Tests edge case awareness: the query returns no rows.)