Home / SQL / Practice problems / Hard

Running total of orders

Hardrow order matters

For each month, show orders and the running_total of orders so far, in month order.

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
monthordersrunning_total
2025-0122
2025-0235
2025-03813
2025-04518
2025-05523
2025-06225
2025-07530
2025-08636
2025-09238
2025-10644
2025-11347
2025-12451

12 rows

Show a solution
WITH m AS (SELECT strftime('%Y-%m', order_date) AS month, COUNT(*) AS orders FROM orders GROUP BY month) SELECT month, orders, SUM(orders) OVER (ORDER BY month) AS running_total FROM m ORDER BY month;

SQL used

New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.