SELECT p.name,vld.qty_available stock_level,COUNT(t.id) sales,SUM(tsl.quantity) units_sold FROM products p LEFT JOIN variation_location_details vld ON p.id=vld.product_id 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" GROUP BY p.id,p.name,vld.qty_available ORDER BY units_sold DESC;