SELECT p.name,COUNT(DISTINCT t.id) as sales,SUM(tsl.quantity) as units_sold FROM products p LEFT JOIN transaction_sell_lines tsl ON p.id=tsl.product_id LEFT JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" AND t.status="final" GROUP BY p.id,p.name ORDER BY units_sold DESC;