Rank Users by Average Transaction Amount with Ties Sharing a Rank
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
For each user with at least one transaction, compute the average transaction amount and rank users by it in descending order. Return `user_id`, `user_name`, `avg_transaction_amount` (rounded to 2 decimal places) and `rank`. Users with equal averages share a rank, and the following rank is skipped.
```hint Choosing the ranking function
Check what your ranking function assigns right after a tie: if two users share rank 2, the next user must get rank 4.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier. Primary key. |
| user_name | VARCHAR(100) | User name. NOT NULL. Not necessarily unique. |
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INT | Transaction identifier. Primary key. |
| user_id | INT | User who made the transaction. NOT NULL, foreign key to `users.user_id`. |
| amount | DECIMAL(10,2) | Transaction amount. NOT NULL, always positive. |
| transaction_date | DATE | Date of the transaction. NOT NULL. |
Keys: `users.user_id` and `transactions.transaction_id` are primary keys. `transactions.user_id` references `users.user_id`.
### Output Contract
- Columns, in this order: `user_id`, `user_name`, `avg_transaction_amount`, `rank`.
- One row per user who has at least one transaction.
- `avg_transaction_amount` is the mean of the user's `amount` values, rounded to 2 decimal places with halves rounded away from zero (PostgreSQL `ROUND` on a `numeric`).
- `rank` is an integer starting at 1, computed on the unrounded average in descending order. Users with equal averages share a rank and the next rank is skipped (1, 2, 2, 4).
- Sort by `rank` ascending, then `user_id` ascending. `user_id` is unique, so the row order is fully determined.
### Example
`users`
| user_id | user_name |
|---|---|
| 1 | Ana |
| 2 | Ben |
| 3 | Cara |
| 4 | Dev |
| 5 | Eli |
| 6 | Fay |
`transactions`
| transaction_id | user_id | amount | transaction_date |
|---|---|---|---|
| 1 | 1 | 50.00 | 2024-02-01 |
| 2 | 2 | 40.00 | 2024-02-01 |
| 3 | 3 | 60.00 | 2024-02-02 |
| 4 | 1 | 30.00 | 2024-02-03 |
| 5 | 4 | 10.00 | 2024-02-03 |
| 6 | 3 | 20.01 | 2024-02-04 |
| 7 | 4 | 10.00 | 2024-02-05 |
| 8 | 5 | 10.00 | 2024-02-05 |
| 9 | 4 | 10.01 | 2024-02-06 |
Expected output:
| user_id | user_name | avg_transaction_amount | rank |
|---|---|---|---|
| 3 | Cara | 40.01 | 1 |
| 1 | Ana | 40.00 | 2 |
| 2 | Ben | 40.00 | 2 |
| 4 | Dev | 10.00 | 4 |
| 5 | Eli | 10.00 | 5 |
Cara's average is 80.01 / 2 = 40.005, the highest, so she ranks first and her average displays as 40.01. Ana (80.00 / 2) and Ben (40.00 / 1) both average exactly 40.00 and share rank 2, so the next rank is 4. Dev averages 30.01 / 3 = 10.0033... and Eli averages 10.00. Both display as 10.00, but Dev's unrounded average is higher, so Dev ranks 4 and Eli ranks 5. Fay has no transactions and does not appear.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `transactions` at most 100,000 rows.
- `amount` is never NULL and always positive.
- Every `transactions.user_id` exists in `users`. Users with no transactions are left out of the result.
- The rank compares unrounded averages. Rounding only affects the displayed `avg_transaction_amount`, so two users can show the same rounded value and still hold different ranks.
- Two users can share a name, so users are identified by `user_id`.
- Ties in rank are ordered by `user_id` ascending.
Overview: Write a SQL query that averages each user's transaction amounts, rounds the result, and ranks users by that average, with tied users sharing a rank and the next rank skipped. It tests joins, GROUP BY, window functions over aggregates, and careful tie handling.