Home / SQL / Practice problems / Medium

Cancellation rate

Medium

Return cancelled_pct: the percentage of orders that were cancelled, rounded to 1 decimal.

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
cancelled_pct
13.7

1 row

Show a solution
SELECT ROUND(100.0 * SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) / COUNT(*), 1) AS cancelled_pct FROM orders;

SQL used

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