Top-Spending Customer per Country
In each country, which customer has spent the most on Apple products, and how much have they spent?
- CTEs
- Window functions
- Joins
- Subqueries
- Aggregation
Challenge brief
Understand the request
International Sales is launching a VIP rewards programme and needs to identify the top-spending Apple customer in each country.
Find the highest-spending customer per country using a CTE and ROW_NUMBER() window function.
Return
- country
- customer_name (full name)
- total_orders
- total_spent (rounded 2)
- last_order_date
Constraints
- Return one top-spending customer per country with orders
- When spend ties, the lower customer ID wins
- Show the largest country-leading spend first and resolve ties by country
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
customers
customer_idINTEGERfirst_nameVARCHAR(50)last_nameVARCHAR(50)countryVARCHAR(50)
orders
order_idINTEGERcustomer_idINTEGERorder_dateDATETIMEtotal_amountREAL
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Two-step pattern: CTE 1 aggregates total spend per customer. CTE 2 uses ROW_NUMBER() OVER (PARTITION BY country ORDER BY total_spent DESC) to rank customers within each country. Outer query: WHERE rn = 1.
Hint 2
CTE customer_stats: JOIN customers to orders, GROUP BY customer, compute total_spent. CTE ranked: add ROW_NUMBER() OVER (PARTITION BY country ORDER BY total_spent DESC). Outer: WHERE rn = 1.
Hint 3
Build question 19 from its business grain: identify the driving rows, add only valid relationships, then apply the required filtering, aggregation, and deterministic ordering.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
WITH customer_stats AS (SELECT c.customer_id, c.first_name || ' ' || c.last_name AS customer_name, c.country, COUNT(o.order_id) AS total_orders, ROUND(SUM(o.total_amount), 2) AS total_spent, MAX(o.order_date) AS last_order_date FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.first_name, c.last_name, c.country), ranked AS (SELECT customer_id, customer_name, country, total_orders, total_spent, last_order_date, ROW_NUMBER() OVER (PARTITION BY country ORDER BY total_spent DESC, customer_id ASC) AS rn FROM customer_stats) SELECT country, customer_name, total_orders, total_spent, last_order_date FROM ranked WHERE rn = 1 ORDER BY total_spent DESC, countryWhy this works
ROW_NUMBER() assigns 1 to the top spender per country. WHERE rn = 1 extracts exactly one row per country. David Brown (USA, $399) is excluded because Alice Johnson outspends him in USA.
Success check
3 countries — Canada: Carol White ($3,798), USA: Alice Johnson ($3,747), UK: Emma Davis ($799)
Expected result
Use this output to verify values, aliases, ordering, and row count.
| country | customer_name | total_orders | total_spent | last_order_date |
|---|---|---|---|---|
| Canada | Carol White | 2 | 3798 | 2024-02-10 14:30:00 |
| USA | Alice Johnson | 3 | 3747 | 2024-02-08 12:00:00 |
| UK | Emma Davis | 1 | 799 | 2024-02-03 10:20:00 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Microsoft
Independent Microsoft-style cloud, productivity, subscription, usage, support, and customer analytics SQL practice.
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Amazon
Independent Amazon-style e-commerce, warehouse, inventory, and customer analytics SQL practice.
Return to the complete interview preparation experience.