A marketing team wants to rank every customer by how much they have spent in total across all their orders. Using the orders table, group orders by customer and return customer_name and total_spend (the sum of all their order amounts), ordered by total_spend descending.
orders
| column | type |
|---|---|
| id | INTEGER |
| customer_id | INTEGER |
| customer_name | TEXT |
| amount | NUMERIC |
| id | customer_id | customer_name | amount |
|---|---|---|---|
| 1 | 1 | Alice | 120.00 |
| 2 | 2 | Bob | 45.00 |
| 3 | 1 | Alice | 200.00 |
| 4 | 3 | Carol | 300.00 |
| 5 | 2 | Bob | 155.00 |
| 6 | 3 | Carol | 50.00 |
| customer_name | total_spend |
|---|---|
| Carol | 350.00 |
| Alice | 320.00 |
| Bob | 200.00 |
Alice has two orders totaling 320.00, Bob has two totaling 200.00, and Carol has two totaling 350.00. The result is sorted by total_spend descending so the biggest spender appears first.