logo

Back to questions

Most Expensive Purchase

Easy

Amazon is trying to identify their high-end customers. To do so, they first need your help to write a query that obtains the most expensive purchase made by each customer. Order the results by the most expensive purchase first.

Table:

Column NameType
transaction_idinteger
customer_idinteger
purchase_amountinteger

Example Input:

transaction_idcustomer_idpurchase_amount
11150
2135.90
32349.99
42199.95
52551.20
6313.30

Example Output:

customer_idpurchase_amount
2551.20
1150
313.30

Explanation:

User 1 have 2 purchases (150 and 35.90) with 150 being the most expensive purchase whereas user 2 have 3 purchases (349.99, 199.95, and 551.20) and 551.20 is their most expensive purchase.

User 3 only have 1 purchase at 13.30 and hence it's their most expensive purchase. The output is then sorted by the most expensive purchase to the least expensive.

PostgreSQL 14

Refer friends to get bonus content & cool prizes.