MySQL solution
SELECT stock_name,\nSUM(CASE WHEN operation = \'Buy\' THEN -1*price\nWHEN operation = \'sell\' THEN price END) as capital_gain_loss\nFROM Stocks\nGROUP BY stock
SELECT stock_name,\nSUM(CASE WHEN operation = \'Buy\' THEN -1*price\nWHEN operation = \'sell\' THEN price END) as capital_gain_loss\nFROM Stocks\nGROUP BY stock_name;
[]