Simple solution with SUM, IF, and GROUP BY
\nSELECT stock_name, SUM(IF(operation="Buy",-1*price,price)) AS capital_gain_loss       \nFROM Stocks\nGROUP BY stock_name`\n\n
```\nSELECT stock_name, SUM(IF(operation="Buy",-1*price,price)) AS capital_gain_loss       \nFROM Stocks\nGROUP BY stock_name`\n```\n
[]