CODE WORKSPACE

Current-row snapshot audit

Return country and paid_rate, the fraction of that country's orders whose status is paid, formatted to 2 decimal places as text with printf('%.2f', ...). Group by buyer country, replacing a missing country with 'unknown'. Order by the paid fraction descending, then country ascending.

Multiply by 1.0 to force decimals, then format: printf('%.2f', 1.0 * SUM(CASE WHEN o.status = 'paid' THEN 1 ELSE 0 END) / COUNT(*)).

Sample input

ordersPer country: unknown 2/2 paid, GB 3/5 paid, US 1/3 paid, IN 0/2 paid.
order_idbuyer_idstatus
10011paid
10041shipped
10091pending
10052paid
10112paid
customersbuyer_id joins to country; buyer 2 has a NULL country (becomes unknown).
customer_idcountry
1US
2null
3GB
4IN
6GB

Expected output

Expected outputDecimal-safe share of paid orders per country, formatted to 2dp text with printf, highest first.
countrypaid_rate
unknown1.00
GB0.60
US0.33
IN0.00

Constraints

JOIN orders to customers on customers.customer_id = orders.buyer_id. Force decimal math with 1.0 * SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) / COUNT(*) so it does not floor to 0, then wrap it in printf('%.2f', ...). Group by COALESCE(country, 'unknown'). Order by the numeric paid fraction DESC, then country ASC.

Expected skills

Safe ratio math (avoiding integer division), formatting a rate to fixed decimals, and grouping on a COALESCE expression.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.