Net Profit per Trading Card
Problem
A collector resells trading cards. Every transaction is logged: a `purchase` records money spent acquiring a card, a `resale` records money received when selling it. Each card may be bought and sold many times.
Table: `card_trades`
| Column | Type |
|-------------|---------|
| card_name | varchar |
| action | text |
| trade_day | int |
| amount | int |
`(card_name, trade_day)` is the primary key. `action` is either `'purchase'` or `'resale'`. `amount` is the money for that single transaction.
The **net profit** of a card is the total received from resales minus the total spent on purchases, summed over all its transactions. A negative value means an overall loss.
Write a query that reports `card_name` and `net_profit` for each card. Return the rows in any order.
Tables
Example rows — the live problem includes the full dataset.
| card_name | action | trade_day | amount |
|---|---|---|---|
| Solar Drake | purchase | 1 | 1000 |
| Tidecaller | purchase | 2 | 10 |
| Solar Drake | resale | 5 | 9000 |
Expected output
Your answer should return 3 rows with the columns card_name, net_profit.
Starter code (SQL)
SELECT *
FROM card_trades;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock