Home / SQL / Practice problems / Medium

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
Show the expected result
order_idtotal
401711.0
201669.0
301647.0
361283.5
161149.0
241077.0
17987.0
6826.0
32822.0
29820.0
39811.0
15808.0
49784.0
7759.0
9743.5
10683.0
8658.0
5498.0
41420.5
43404.0
42402.0
12381.0
37336.0
27326.0
51321.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.