cswork / libraryOpen practice workspace

Coding questions · Amazon

Calculate Regional Revenue and Identify Top Customers

This question evaluates proficiency with SQL data manipulation concepts such as joins, aggregation (SUM/GROUP BY), and window functions for ranking customers by spending.

Customers

customer_idnameregion
1AliceEast
2BobWest
3CarolEast

Sales

sale_idcustomer_idproduct_idorder_dateamount
1011P12023-01-10120.00
1022P22023-01-11250.00
1031P32023-01-1280.00
Scenario

An e-commerce analyst needs to compute regional revenue and then list the top-3 highest-spending customers in every region for a dashboard.

Question

Write an SQL query that joins the Customers and Sales tables, groups by region, and returns total revenue per region. 2. Using window functions, extend the query to return the three customers with the highest total spending in their respective regions (use RANK() OVER(PARTITION BY … ORDER BY … DESC)).

Hints

Think INNER vs. LEFT JOIN, SUM(amount), GROUP BY region, and RANK() window function with PARTITION BY region.

Regional Revenue and Top Customers per Region

You are given two tables, Customers and Sales. First, write an SQL query that joins Customers and Sales to compute total revenue per region. Then extend your solution (using window functions) to return, for each region, the three customers with the highest total spending in that region. The final result should include: region, total regional revenue, customer_id, customer_name, that customer's total revenue, and their rank within the region by spending. Customers with no sales should still appear with 0 spending if they fall within the top 3 of their region (for example, when there are fewer than three customers in a region). Use RANK() OVER (PARTITION BY region ORDER BY total_spent DESC).

Table Customers

columntypeconstraints
customer_idINTEGERPRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
regionVARCHAR(50)NOT NULL

Sample data:

customer_idnameregion
1AliceEast
2BobWest
3CarolEast

Table Sales

columntypeconstraints
sale_idINTEGERPRIMARY KEY, NOT NULL
customer_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Customers(customer_id)
product_idVARCHAR(50)NOT NULL
order_dateDATENOT NULL
amountDECIMAL(10,2)NOT NULL

Sample data:

amountcustomer_idorder_dateproduct_idsale_id
12012023-01-10P1101
25022023-01-11P2102
8012023-01-12P3103

Expected output

For each region, show total regional revenue and up to the top three customers in that region ranked by their total spending. Customers with no sales should appear with 0 spending if they rank within the top three because of limited customer count in the region.

customer_idcustomer_namecustomer_rankcustomer_total_revenueregionregion_total_revenue
1Alice1200East200
3Carol20East200
2Bob1250West250

Hints

  1. Decide between INNER JOIN and LEFT JOIN depending on whether to include customers with no sales.
  2. Use SUM(amount) with GROUP BY region to compute regional revenue.
  3. Use a window function like RANK() OVER (PARTITION BY region ORDER BY total_spent DESC) to rank customers by spending within each region.