Order Values
Finance at Whetcode Supply is reconciling the shop's own records against the payment provider's statement, and starts at the top: the five most valuable orders, since a discrepancy there costs more than a discrepancy anywhere else.
An order row does not say what it was worth. It says which item was bought and how many, and the money lives in the catalogue. Two orders sit on the boundary of the top five with the same value, so the sheet needs a rule deciding which of them makes the cut.
orders — one row per order. customer_id is the id of the customer who placed it, product_id the id of the item bought, quantity how many units of that item, and order_date the day it was placed. An order row covers one item only.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
products — the catalogue, one row per item on sale. price is what a single unit costs, in dollars.
| id | name | category | price |
|---|---|---|---|
| 1 | Wireless Mouse | electronics | 25.0 |
| 2 | Standing Desk | furniture | 350.0 |
| 3 | Desk Lamp | furniture | 40.0 |
| 4 | Mechanical Keyboard | electronics | 85.0 |
| 5 | Notebook Set | office | 12.0 |
The tables already exist in the database — there is nothing to create or load.
Task: Write a query returning two columns, id (the order's own id) and order_value (what that order was worth: the item's price times the number of units, rounded to two decimal places), returning only the five most valuable orders, largest value first. When two orders have the same value, put the smaller id first — this also decides which of the two orders worth 85 appears and which is left out.
Example output — shape only, on invented orders. The top two are worth the same, so the smaller id goes first:
| id | order_value |
|---|---|
| 41 | 640 |
| 43 | 640 |
| 38 | 495 |
| 46 | 310 |
| 52 | 175 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in