Simple and Easy to understand solution
Approach\n1. Selecting stock name\n2. SUM of the price (If operation is Buy then multiply with "-1" else return unchanged price value)\n3. Group by stock_name\n
# Approach\n1. Selecting stock name\n2. SUM of the price (If operation is Buy then multiply with "-1" else return unchanged price value)\n3. Group by stock_name\n\n\n# Code\n```\nSELECT \n    stock_name,\n    SUM(CASE WHEN operation = "Buy" THEN (-1 * price)\n        ELSE price END) AS capital_gain_loss\nFROM Stocks\nGROUP BY stock_name;\n```
['MySQL']