short/easy to understand solution using sum & if
\n\nselect stock_name , sum(if(operation = \'Buy\',-Price,Price)) as capital_gain_loss\nfrom stocks\ngroup by stock_name\n
\n```\nselect stock_name , sum(if(operation = \'Buy\',-Price,Price)) as capital_gain_loss\nfrom stocks\ngroup by stock_name\n```
['MySQL']