Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Drivers of Failed Payments

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rank Drivers of Failed Payments. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

FinFlow wants to understand which account and transaction attributes are most associated with failed payments in June 2024.

Write a SQL query to return the top-ranked value for each of these dimensions: country_code, payment_method, risk_tier, and surface_name.

Requirements

  1. Use only transactions from 2024-06-01 through 2024-06-30.
  2. For each dimension value, calculate:
    • total_transactions
    • failed_transactions where status = 'failed'
    • failure_rate = failed_transactions / total_transactions, rounded to 4 decimals
  3. Treat missing dimension values as 'Unknown'.
  4. Only keep dimension values with at least 2 failed transactions.
  5. Rank values within each dimension by:
    1. failed_transactions descending
    2. total_transactions descending
    3. driver_value ascending
  6. Return only rows with failure_rank = 1.

Schema

transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
account_idINTAccount associated with the transaction
transaction_tsTIMESTAMPTimestamp when the transaction was attempted
payment_methodVARCHAR(50)Payment method used for the transaction
statusVARCHAR(20)Final transaction status
failure_reasonVARCHAR(100)Failure reason when a transaction fails
amountNUMERIC(10,2)Transaction amount
accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
country_codeVARCHAR(2)Country code for the account
risk_tierVARCHAR(20)Risk segment assigned to the account
merchant_idINTMerchant linked to the account
merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
surface_nameVARCHAR(50)Product surface where the merchant operates
merchant_nameVARCHAR(100)Merchant display name
Tablestransactionsaccountsmerchants
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results