Total of each order
Mediumrow order matters
Show each order_id and its total (sum of quantity × unit_price), largest first.
Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)
Your query✔ Solved
Show the expected result
| order_id | total |
|---|---|
| 40 | 1711.0 |
| 20 | 1669.0 |
| 30 | 1647.0 |
| 36 | 1283.5 |
| 16 | 1149.0 |
| 24 | 1077.0 |
| 17 | 987.0 |
| 6 | 826.0 |
| 32 | 822.0 |
| 29 | 820.0 |
| 39 | 811.0 |
| 15 | 808.0 |
| 49 | 784.0 |
| 7 | 759.0 |
| 9 | 743.5 |
| 10 | 683.0 |
| 8 | 658.0 |
| 5 | 498.0 |
| 41 | 420.5 |
| 43 | 404.0 |
| 42 | 402.0 |
| 12 | 381.0 |
| 37 | 336.0 |
| 27 | 326.0 |
| 51 | 321.5 |
51 rows (showing 25)
Show a solution
SELECT order_id, SUM(quantity * unit_price) AS total FROM order_items GROUP BY order_id ORDER BY total DESC, order_id;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.