Solution through IF and SUM
\nselect stock_name, \n       sum(\n           if(operation = \'Buy\', -1*price, price)\n           ) as capital_gain_loss\nfrom Stocks\ngroup by stock_name\n
```\nselect stock_name, \n       sum(\n           if(operation = \'Buy\', -1*price, price)\n           ) as capital_gain_loss\nfrom Stocks\ngroup by stock_name\n```
['MySQL']