Easy solution
\nselect stock_name, \nsum(if(operation = \'buy\', -price, price)) \nas capital_gain_loss from stocks \ngroup by stock_name;\n
```\nselect stock_name, \nsum(if(operation = \'buy\', -price, price)) \nas capital_gain_loss from stocks \ngroup by stock_name;\n```
['MySQL']