Home / SQL / Practice problems / Hard

Average order value

Hard

Return avg_order_value: the average total of delivered orders, rounded to 2 decimals.

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
avg_order_value
442.73

1 row

Show a solution
WITH t AS (SELECT o.order_id, SUM(i.quantity * i.unit_price) AS total FROM orders o JOIN order_items i ON i.order_id = o.order_id WHERE o.status = 'delivered' GROUP BY o.order_id) SELECT ROUND(AVG(total), 2) AS avg_order_value FROM t;

SQL used

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