SELECT c.name category,COUNT(DISTINCT CASE WHEN t.type="sell_return" THEN t.id END)*100.0/COUNT(DISTINCT CASE WHEN t.type="sell" THEN t.id END) return_rate FROM categories c JOIN products p ON c.id=p.category_id JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id GROUP BY c.id,c.name ORDER BY return_rate DESC;