Easiest solution using SUM and IF
\nselect\n    stock_name, \n    sum(if(operation=\'Buy\', -price, price)) \n    as capital_gain_loss \nfrom stocks \ngroup by stock_name\n\n\n#Syntax of IF - if
```\nselect\n    stock_name, \n    sum(if(operation=\'Buy\', -price, price)) \n    as capital_gain_loss \nfrom stocks \ngroup by stock_name\n```\n\n#Syntax of IF - if(condition, outcome if true, outcome if false)\nOr else CASE WHEN can be used. But it\'s generally used where there are multiple cases/conditions
['MySQL']