SQL Interview Questions – Monthly Transactions

Spread the love

Problem Description –

Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.

Return the result table in any order.

The query result format is in the following example.

Difficulty Level – Medium

Problem Link – Monthly Transactions

Solution –

SELECT
    DATE_FORMAT(trans_date, '%Y-%m') as month,
    country,
    COUNT(*) as trans_count,
    SUM(CASE WHEN state ='approved' THEN 1 ELSE 0 END) as approved_count,
    SUM(amount) as trans_total_amount,
    SUM(CASE WHEN state='approved' THEN amount ELSE 0 END) as approved_total_amount
FROM Transactions
GROUP BY 1, 2

Rating: 1 out of 5.

Leave a Reply