Interview Questions/SQL/Monthly Transactions I

Monthly Transactions I

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a table of financial transactions. Each transaction has a country, a state (either 'approved' or 'declined'), an amount, and a date. Write a SQL query to find for each month and country: the total number of transactions, the number of approved transactions, the total transaction amount, and the total approved transaction amount. Return the result in any order.

Database Schema

Transactions

Column NameTypeDescription
idINTPrimary key
countryVARCHARCountry of transaction
stateENUM'approved' or 'declined'
amountINTTransaction amount
trans_dateDATEDate of transaction

Example

Transactions

idcountrystateamounttrans_date
121USapproved10002018-12-18
122USdeclined20002018-12-19
123USapproved20002019-01-01
124DEapproved20002019-01-07

Output

monthcountrytrans_countapproved_counttrans_total_amountapproved_total_amount
2018-12US2130001000
2019-01DE1120002000
2019-01US1120002000

Explanation:

Group by month (YYYY-MM) and country. Count all transactions, count approved ones, sum all amounts, sum approved amounts.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Transactions

idcountrystateamounttrans_date
121USapproved10002018-12-18
122USdeclined20002018-12-19
123USapproved20002019-01-01
124DEapproved20002019-01-07

Output

monthcountrytrans_countapproved_counttrans_total_amountapproved_total_amount
2018-12US2130001000
2019-01DE1120002000
2019-01US1120002000