FinEdge wants to identify its highest-volume users in each country based on transaction activity. Write a PostgreSQL query to return the top 3 users per country by total transaction volume.
Requirements
- Join
users and transactions.
- Calculate each user's total transaction volume per country using the sum of
amount.
- Exclude transactions where
amount is NULL.
- Return the top 3 users within each country using a window function.
- Order the final output by
country, then rank, then user_id.
Table Definitions
users
| column | type | description |
|---|
| user_id | INT | Primary key for each user |
| user_name | VARCHAR(100) | User display name |
| country | VARCHAR(50) | User country |
| signup_date | DATE | Date the user registered |
transactions
| column | type | description |
|---|
| transaction_id | INT | Primary key for each transaction |
| user_id | INT | References users.user_id |
| amount | DECIMAL(10,2) | Transaction amount |
| transaction_date | DATE | Transaction date |