✅ Simple solution using GROUP BY and CASE WHEN ✅
\n# Approach\nUsed GROUP BY to group the same stocks.\nThen used CASE WHEN to split the Sell and Buy option, then finally added those values using SUM.\nBy Subt
\n# Approach\nUsed GROUP BY to group the same stocks.\nThen used CASE WHEN to split the Sell and Buy option, then finally added those values using SUM.\nBy Subtraction these two values, simply we will get the output.\n\n\n# Code\n```postgresql []\n-- Write your PostgreSQL query statement below\nSELECT stock_name, \n(SUM(CASE WHEN operation = \'Sell\' THEN price ELSE 0 END) - SUM (CASE WHEN operation = \'Buy\' THEN price ELSE 0 END)) as capital_gain_loss \nFROM Stocks GROUP BY stock_name\n```
['PostgreSQL']