Capital Gain/Loss - Problem

Given a table Stocks with columns stock_name, operation, operation_day, and price, calculate the capital gain/loss for each stock.

The capital gain/loss is the total profit or loss after buying and selling the stock one or many times. This is calculated as:

  • Sum of all Sell prices minus sum of all Buy prices for each stock
  • Positive value = capital gain, negative value = capital loss

Note: Each Buy operation has a corresponding Sell operation and vice versa.

Table Schema

Stocks
Column Name Type Description
stock_name PK varchar Name of the stock
operation PK enum Type of operation: 'Buy' or 'Sell'
operation_day PK int Day when the operation occurred
price int Price at which the operation was executed
Primary Key: (stock_name, operation_day)
Note: Each row represents a single buy or sell operation for a stock. All buy operations have corresponding sell operations.

Input & Output

Example 1 — Multiple Stock Transactions
Input Table:
stock_name operation operation_day price
Corona Masks Buy 1 10
Corona Masks Sell 2 12
Handbags Buy 17 30
Handbags Sell 29 7
Output:
stock_name capital_gain_loss
Corona Masks 2
Handbags -23
💡 Note:

Corona Masks: Bought for 10, sold for 12 → gain of 2

Handbags: Bought for 30, sold for 7 → loss of 23 (shown as -23)

Example 2 — Multiple Buy/Sell Operations
Input Table:
stock_name operation operation_day price
Apple Buy 1 100
Apple Buy 2 110
Apple Sell 3 120
Apple Sell 4 130
Output:
stock_name capital_gain_loss
Apple 40
💡 Note:

Apple: Total bought: 100 + 110 = 210, Total sold: 120 + 130 = 250 → gain of 40

Example 3 — Single Stock Break-even
Input Table:
stock_name operation operation_day price
Tesla Buy 10 500
Tesla Sell 15 500
Output:
stock_name capital_gain_loss
Tesla 0
💡 Note:

Tesla: Bought for 500, sold for 500 → no gain or loss (0)

Constraints

  • 1 ≤ stock_name.length ≤ 20
  • operation is either 'Buy' or 'Sell'
  • 1 ≤ operation_day ≤ 1000
  • 1 ≤ price ≤ 1000
  • Each Buy operation has a corresponding Sell operation

Visualization

Tap to expand
Capital Gain/Loss Calculator INPUT stock op price AAPL Buy 1000 AAPL Sell 2000 MSFT Buy 1500 MSFT Buy 500 MSFT Sell 1800 Buy = Subtract Sell = Add Stocks Table with Buy/Sell operations Gain = SUM(Sell) - SUM(Buy) Per stock calculation ALGORITHM STEPS 1 Group by Stock GROUP BY stock_name 2 Sum Sell Prices WHERE operation='Sell' 3 Sum Buy Prices WHERE operation='Buy' 4 Calculate Gain/Loss Sell_Total - Buy_Total AAPL Calculation: 2000 (Sell) - 1000 (Buy) = 1000 MSFT Calculation: 1800 - (1500+500) = -200 FINAL RESULT stock gain/loss AAPL +1000 MSFT -200 Capital Gain/Loss Summary AAPL: Profit MSFT: Loss Net Position: +1000 - 200 = +800 Key Insight: Use conditional aggregation with CASE WHEN to sum Sell prices positively and Buy prices negatively. SQL: SUM(CASE WHEN operation='Sell' THEN price ELSE -price END) calculates gain/loss in one pass. TutorialsPoint - Capital Gain/Loss | Optimal Solution
Asked in
Amazon 28 Microsoft 22 Apple 18 Goldman Sachs 15
23.4K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen