Knowledge Guide
HomeDatabasesSQL Practice Problems

Monthly Transactions II

Problem

Table: Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| id             | int     |
| country        | varchar |
| state          | enum    |
| amount         | int     |
| trans_date     | date    |
+----------------+---------+
id is the column of unique values of this table.
The table has information about incoming transactions.
The state column is an ENUM (category) of type ["approved", "declined"].

Table: Chargebacks

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| trans_id       | int     |
| trans_date     | date    |
+----------------+---------+
Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
trans_id is a foreign key (reference column) to the id column of Transactions table.
Each chargeback corresponds to a transaction made previously even if they were not approved.

Problem Definition

Write a solution to the number of approved transactions and their total amount, the number of chargebacks, and their total amount find for each month and country. Return the records in the order of date.

Note: In your solution, given the month and country, ignore rows with all zeros.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To determine the number of approved transactions and their total amount, the number of chargebacks and their total amount for each month and country, we can follow a structured approach that leverages SQL's aggregation and conditional functions. This ensures accurate and efficient computation by combining relevant data from the Transactions and Chargebacks tables.

SQL Query

SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM (SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved") s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;

Step-by-Step Approach

Step 1: Combine Chargebacks with Approved Transactions

Merge the Chargebacks and Transactions tables to create a unified dataset that distinguishes chargebacks from approved transactions. This involves labeling chargebacks with a unique state identifier and ensuring that only approved transactions are included from the Transactions table.

SQL Query:

SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved";

Explanation:

Output After Step 1:

Based on the provided input data, the combined dataset (s) will be:

monthcountrystateamount
2019-05USback2000
2019-06USback1000
2019-09USback5000
2019-05USapproved1000
2019-06USapproved3000
2019-06USapproved5000

Explanation of Output:

Step 2: Aggregate Data by Month and Country

For each combination of month and country, calculate:

SQL Query:

SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM ( -- Combined Data from Step 1 SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved" ) s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;

Explanation:

Output After Step 2:

Based on the combined dataset from Step 1, the aggregation would yield:

monthcountryapproved_countapproved_amountchargeback_countchargeback_amount
2019-06US2800011000
2019-05US1100012000
2019-09US0015000

Explanation of Output:

Note:

Step 3: Order the Results by Month

Ensure that the final output is sorted in chronological order based on the month to provide a clear and organized view of the data over time.

SQL Query:

ORDER BY month;

Explanation:

Final Output:

Based on the aggregated data from Step 2, the final output after ordering would be:

monthcountryapproved_countapproved_amountchargeback_countchargeback_amount
2019-05US1100012000
2019-06US2800011000
2019-09US0015000

Explanation of Output:

🤖 Don't fully get this? Learn it with Claude

Stuck on Monthly Transactions II? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Monthly Transactions II** (Databases) and want to truly understand it. Explain Monthly Transactions II from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Monthly Transactions II** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Monthly Transactions II** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Monthly Transactions II** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes