Table Schema
Inspect
Table
| Column | Type |
|---|---|
| txn_id | integer |
| merchant_id | varchar |
| amount | integer |
Find the top 2 merchants by total transaction amount.
Table Schema
| Column | Type |
|---|---|
| txn_id | integer |
| merchant_id | varchar |
| amount | integer |
Sample Data
| txn_id | merchant_id | amount |
|---|---|---|
| 1 | M1 | 100 |
| 2 | M2 | 200 |
| 3 | M1 | 300 |
| 4 | M3 | 400 |
| 5 | M2 | 150 |
| 6 | M1 | 250 |
| merchant_id | total_amount |
|---|---|
| M1 | 650 |
| M3 | 400 |
SQL Editor
| merchant_id | total_amount |
|---|---|
| M1 | 650 |
| M3 | 400 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT merchant_id, SUM(amount) AS total_amount FROM transactions GROUP BY merchant_id ORDER BY total_amount DESC LIMIT 2;
Explanation
Group transactions by merchant.
Sum the amount for each merchant.
Return the two highest totals.
Related Questions