Hirebeat wants to identify its highest-spending users in each country. Write a PostgreSQL query that calculates each user's total purchase spend, ranks users within their country, and returns the top three users per country.
Requirements
- Join users to purchases and calculate total spend per user.
- Include users with no purchases, treating their total spend as
0.
- Use a window function to rank users independently within each country.
- Return at most three users per country, ordered by country and descending total spend. Break spending ties with the smaller
user_id.