MYSQL solution using sum,if,groupby
\n\nselect \n    stock_name , \n    sum(if(operation=\'buy\',-price,price)) \n    as capital_gain_loss  \nfrom stocks \ngroup by stock_name ;\n
\n```\nselect \n    stock_name , \n    sum(if(operation=\'buy\',-price,price)) \n    as capital_gain_loss  \nfrom stocks \ngroup by stock_name ;\n```
['MySQL']