Money moved by payment type

reshaping

Money moved by payment type

PhonePe SQL Interview Question

PhonePe makes money on merchant payments, breaks even on bill payments and loses a little on transfers between people. A user who sends money to friends all day is a cost; one who pays merchants is a profit. Telling them apart means knowing how much each user moved of each kind, and a failed transaction moved nothing.

Return the user_id, and the money that went through successfully for each kind as p2p_amount, merchant_amount and bill_amount. A kind a user never used counts as zero.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

phonepe_transactionsTable30 rows

Column NameType
txn_idBIGINT
user_idVARCHAR
txn_atTIMESTAMP
txn_typeVARCHAR
amountDOUBLE
statusVARCHAR

phonepe_transactionsExample Input

txn_iduser_idtxn_attxn_typeamountstatus
8001U012024-09-02 09:14:00p2p500success
8002U012024-09-02 09:40:00merchant1250failed
8003U012024-09-02 10:05:00merchant1250failed
8004U012024-09-02 10:31:00merchant1250success
8005U012024-09-02 13:22:00bill2300success
8006U012024-09-03 08:10:00p2p750failed
8007U012024-09-03 11:48:00p2p750success
8027U052024-09-02 06:40:00merchant150success
8028U052024-09-02 18:25:00p2p2200success
8029U052024-09-03 10:55:00bill1050failed
8030U052024-09-03 11:21:00bill1050success

Example Output

user_idp2p_amountmerchant_amountbill_amount
U01125012502300
U0522001501050

Explanation

U01 moved 1250 in transfers, 1250 to merchants and 2300 in bills. The merchant figure counts only the attempt that succeeded, not the two that failed ahead of it, because a failed attempt moved no money.

The example above is a small slice of the data. Your query runs against the full tables.