SELECT p.name,DATEDIFF(CURRENT_DATE,p.created_at) age_days,COUNT(t.id) sales,SUM((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)*tsl.quantity) profit FROM products p JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY p.id,p.name,p.created_at ORDER BY profit DESC;