Totals of delivered orders
Medium · Online shop database · JOIN, GROUP BY, COUNT, SUM, AVG
An order's total is the sum of quantity times unit_price over its rows in order_items. Use the unit_price stored with each item, which is what the customer paid; some prices have changed since.
Return the id, order_date and total, rounded to 2 decimals with round, of every order with status delivered, in that order, sorted by order id.
Expected result
| id | order_date | total |
|---|---|---|
| 1 | 2025-02-03 | 34.96 |
| 2 | 2025-02-14 | 220.98 |
| 3 | 2025-03-01 | 89.5 |
| 5 | 2025-04-05 | 70.99 |
| 6 | 2025-04-18 | 82 |
| 7 | 2025-05-02 | 102.99 |
| 8 | 2025-05-27 | 78 |
| 10 | 2025-06-30 | 82.43 |
| 11 | 2025-07-15 | 119 |
| 12 | 2025-08-08 | 52.88 |
| 13 | 2025-08-21 | 44 |
| 15 | 2025-10-02 | 18.5 |
| 16 | 2025-10-19 | 93.99 |
| 18 | 2025-11-28 | 39 |
| 19 | 2025-12-12 | 59.48 |
| 20 | 2025-12-20 | 96 |
| 21 | 2026-01-08 | 36 |
| 22 | 2026-01-26 | 199 |
| 23 | 2026-02-11 | 19.47 |
| 24 | 2026-02-25 | 39 |
| 25 | 2026-03-14 | 146.5 |
The online shop tables
customers: 18 rows, first 5 shown
| id | name | city | country | signup_date | phone |
|---|---|---|---|---|---|
| 1 | Ravi Kumar | Bengaluru | India | 2025-01-12 | +91 98450 12345 |
| 2 | Emily Johnson | Austin | USA | 2025-01-25 | +1 512 555 0142 |
| 3 | Hiroshi Sato | Osaka | Japan | 2025-02-08 | NULL |
| 4 | Ana Souza | Sao Paulo | Brazil | 2025-02-19 | +55 11 91234 5678 |
| 5 | Priya Sharma | Mumbai | India | 2025-03-03 | +91 99200 54321 |
order_items: 40 rows, first 5 shown
| id | order_id | product_id | quantity | unit_price |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 22.99 |
| 2 | 1 | 9 | 3 | 3.99 |
| 3 | 2 | 4 | 1 | 189 |
| 4 | 2 | 16 | 2 | 15.99 |
| 5 | 3 | 2 | 1 | 89.5 |
orders: 28 rows, first 5 shown
| id | customer_id | order_date | status |
|---|---|---|---|
| 1 | 1 | 2025-02-03 | delivered |
| 2 | 2 | 2025-02-14 | delivered |
| 3 | 3 | 2025-03-01 | delivered |
| 4 | 1 | 2025-03-22 | cancelled |
| 5 | 4 | 2025-04-05 | delivered |
products: 18 rows, first 5 shown
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 24.99 | 120 |
| 2 | Mechanical Keyboard | Electronics | 89.5 | 45 |
| 3 | USB-C Hub | Electronics | 39 | 80 |
| 4 | Noise Cancelling Headphones | Electronics | 199 | 25 |
| 5 | Intro to Statistics | Books | 48 | 60 |
-- Write your SQL here