SQL
Lab
Google Google Pay Interview Question 04

Merchant
Revenue

Find the top 2 merchants by total transaction amount.

Table Schema

Inspect
Table

interactive
ColumnType
txn_idinteger
merchant_idvarchar
amountinteger

Sample Data

Input
Output

Sample Input: transactions
txn_idmerchant_idamount
1M1100
2M2200
3M1300
4M3400
5M2150
6M1250
Expected Output
merchant_idtotal_amount
M1650
M3400

SQL Editor

Run
Query

postgresql
Waiting for query

merchant_idtotal_amount
M1650
M3400

Hints

Unlock
Clues

Hint 01: Revenue is the sum of amount.
Hint 02: Aggregate by merchant_id.
Hint 03: Sort high to low and limit to 2.

Solution

Locked
Answer

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

Step By
Step

01

Group transactions by merchant.

02

Sum the amount for each merchant.

03

Return the two highest totals.

Related Questions

Keep
Solving