MySQL solution two approaches| Faster Solution beats 96%
Upvote is appreciated\nApproach -1: Using Subquery\n\nselect stock_name, \n\t(select sum(price) \n\t\tfrom stocks \n\t\twhere operation=\'Sell\'\n\t\t\tand stoc
##### Upvote is appreciated\nApproach -1: Using Subquery\n```\nselect stock_name, \n\t(select sum(price) \n\t\tfrom stocks \n\t\twhere operation=\'Sell\'\n\t\t\tand stock_name=S1.stock_name ) -\n\t(select sum(price) \n\t\tfrom stocks \n\t\twhere operation=\'Buy\'\n\t\t\tand stock_name=S1.stock_name) as capital_gain_loss \n\tfrom stocks S1\n\tgroup by stock_name\n```\n\nApproach-2 Using Case Statement (Faster Approach)\n\n```\nselect stock_name, \n\tsum(case \n\t\twhen operation = \'Buy\' then -price \n\t\telse price \n\t\tend) as capital_gain_loss \nfrom Stocks \n\tgroup by stock_name\n```
['MySQL']