The simplest Solution
Code\n\nSELECT\n    stock_name,\n    SUM((operation = \'Sell\')::int * price) - \n        SUM((operation = \'Buy\')::int * price) capital_gain_loss\nFROM Stocks
# Code\n```\nSELECT\n    stock_name,\n    SUM((operation = \'Sell\')::int * price) - \n        SUM((operation = \'Buy\')::int * price) capital_gain_loss\nFROM Stocks\nGROUP BY 1\n```
['PostgreSQL']