SIMPLE SOLUTION USING SELECT+SUM+IF: BEAT 100%!
\n# Code\n\nSELECT stock_name, SUM(IF(operation="Buy",-price,price)) AS capital_gain_loss\nFROM Stocks\nGROUP BY stock_name\n
\n# Code\n```\nSELECT stock_name, SUM(IF(operation="Buy",-price,price)) AS capital_gain_loss\nFROM Stocks\nGROUP BY stock_name\n```
['MySQL']