Average transaction amount per card type for each month of the previous year

Quick Overview

Write a SQL query that averages credit card transaction amounts by card type for each month of the previous calendar year. It tests joins, date filtering relative to the current date, grouping on an extracted month, and rounding the result.

Average transaction amount per card type for each month of the previous year

Company: Capital One

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You are given two tables, `cards` and `transactions`. The `cards` table stores each card's `card_id` and its `card_type` (one of `Gold`, `Silver` or `Platinum`). The `transactions` table records every transaction with a `transaction_id`, the `card_id` used, the `transaction_date` and the `amount` charged. Write a SQL query to compute the average transaction amount per card type for each month of the previous calendar year. The previous year is defined relative to the current date: if today is in 2025, the query covers 2024-01-01 through 2024-12-31. ```hint Filtering the year Derive the first day of last year and the first day of this year from the current date, then keep the rows whose date falls in that half-open range. ``` ### Input Table(s) Table `cards` (primary key `card_id`): | Column | Type | Meaning | |---|---|---| | card_id | INT | Card identifier, primary key | | card_type | VARCHAR(20) | `Gold`, `Silver` or `Platinum`, never NULL | Table `transactions` (primary key `transaction_id`, `card_id` is a foreign key to `cards.card_id`): | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier, primary key | | card_id | INT | Card used for the transaction | | transaction_date | DATE | Date of the transaction, never NULL | | amount | DECIMAL(10,2) | Amount charged, positive, up to 2 decimal places, never NULL | ### Output Contract One row for each (month, card type) pair that has at least one transaction in the previous calendar year: | Column | Type | Meaning | |---|---|---| | month | INTEGER | Month number, 1 to 12 | | card_type | VARCHAR | Card type | | avg_amount | NUMERIC | Mean of `amount` over that month's transactions for that card type, rounded to 2 decimal places | Sort by `month` ascending, then by `card_type` ascending. Each (month, card_type) pair appears once, so this order is fully determined. ### Example Assume today's date is 2025-03-10, so the previous calendar year is 2024. `cards`: ```text +---------+-----------+ | card_id | card_type | +---------+-----------+ | 1 | Gold | | 2 | Platinum | | 3 | Gold | | 4 | Silver | +---------+-----------+ ``` `transactions`: ```text +----------------+---------+------------------+--------+ | transaction_id | card_id | transaction_date | amount | +----------------+---------+------------------+--------+ | 1 | 1 | 2024-01-15 | 120.00 | | 2 | 1 | 2024-06-01 | 300.00 | | 3 | 2 | 2024-06-15 | 800.50 | | 4 | 3 | 2024-06-20 | 100.00 | | 5 | 3 | 2024-06-28 | 50.50 | | 6 | 4 | 2024-12-31 | 45.00 | | 7 | 1 | 2023-06-01 | 999.99 | | 8 | 2 | 2025-01-01 | 1.00 | +----------------+---------+------------------+--------+ ``` Expected result: ```text +-------+-----------+------------+ | month | card_type | avg_amount | +-------+-----------+------------+ | 1 | Gold | 120.00 | | 6 | Gold | 150.17 | | 6 | Platinum | 800.50 | | 12 | Silver | 45.00 | +-------+-----------+------------+ ``` Transactions 7 (2023) and 8 (2025) fall outside 2024 and are dropped. June Gold has three transactions from two Gold cards, 300.00, 100.00 and 50.50, so its average is 450.50 / 3 = 150.1666..., which rounds to 150.17. ### Constraints and Clarifications - `cards` has 1 to 10,000 rows and `transactions` has 1 to 100,000 rows. - The average is taken over transactions. Every transaction counts once, so a card with several transactions in a month carries more weight than a card with one. - Months with no transactions, and card types with no transactions in a given month, do not appear. Do not output rows with a zero or NULL average. - "Current date" means the database's `CURRENT_DATE`. Transactions in the current year, or two or more years back, are excluded. - `card_type`, `transaction_date` and `amount` are never NULL. A transaction whose `card_id` matches no row in `cards` has no card type and is excluded. - Two transactions with the same card, date and amount are separate rows with different `transaction_id` values, and both count.

Overview: Write a SQL query that averages credit card transaction amounts by card type for each month of the previous calendar year. It tests joins, date filtering relative to the current date, grouping on an extracted month, and rounding the result.

Capital One logo
Capital One
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

You are given two tables, cards and transactions. The cards table stores each card's card_id and its card_type (one of Gold, Silver or Platinum). The transactions table records every transaction with a transaction_id, the card_id used, the transaction_date and the amount charged.

Write a SQL query to compute the average transaction amount per card type for each month of the previous calendar year. The previous year is defined relative to the current date: if today is in 2025, the query covers 2024-01-01 through 2024-12-31.

Input Table(s)

Table cards (primary key card_id):

ColumnTypeMeaning
card_idINTCard identifier, primary key
card_typeVARCHAR(20)Gold, Silver or Platinum, never NULL

Table transactions (primary key transaction_id, card_id is a foreign key to cards.card_id):

ColumnTypeMeaning
transaction_idINTTransaction identifier, primary key
card_idINTCard used for the transaction
transaction_dateDATEDate of the transaction, never NULL
amountDECIMAL(10,2)Amount charged, positive, up to 2 decimal places, never NULL

Output Contract

One row for each (month, card type) pair that has at least one transaction in the previous calendar year:

ColumnTypeMeaning
monthINTEGERMonth number, 1 to 12
card_typeVARCHARCard type
avg_amountNUMERICMean of amount over that month's transactions for that card type, rounded to 2 decimal places

Sort by month ascending, then by card_type ascending. Each (month, card_type) pair appears once, so this order is fully determined.

Example

Assume today's date is 2025-03-10, so the previous calendar year is 2024.

cards:

+---------+-----------+
| card_id | card_type |
+---------+-----------+
| 1       | Gold      |
| 2       | Platinum  |
| 3       | Gold      |
| 4       | Silver    |
+---------+-----------+

transactions:

+----------------+---------+------------------+--------+
| transaction_id | card_id | transaction_date | amount |
+----------------+---------+------------------+--------+
| 1              | 1       | 2024-01-15       | 120.00 |
| 2              | 1       | 2024-06-01       | 300.00 |
| 3              | 2       | 2024-06-15       | 800.50 |
| 4              | 3       | 2024-06-20       | 100.00 |
| 5              | 3       | 2024-06-28       | 50.50  |
| 6              | 4       | 2024-12-31       | 45.00  |
| 7              | 1       | 2023-06-01       | 999.99 |
| 8              | 2       | 2025-01-01       | 1.00   |
+----------------+---------+------------------+--------+

Expected result:

+-------+-----------+------------+
| month | card_type | avg_amount |
+-------+-----------+------------+
| 1     | Gold      | 120.00     |
| 6     | Gold      | 150.17     |
| 6     | Platinum  | 800.50     |
| 12    | Silver    | 45.00      |
+-------+-----------+------------+

Transactions 7 (2023) and 8 (2025) fall outside 2024 and are dropped. June Gold has three transactions from two Gold cards, 300.00, 100.00 and 50.50, so its average is 450.50 / 3 = 150.1666..., which rounds to 150.17.

Constraints and Clarifications

  • cards has 1 to 10,000 rows and transactions has 1 to 100,000 rows.
  • The average is taken over transactions. Every transaction counts once, so a card with several transactions in a month carries more weight than a card with one.
  • Months with no transactions, and card types with no transactions in a given month, do not appear. Do not output rows with a zero or NULL average.
  • "Current date" means the database's CURRENT_DATE . Transactions in the current year, or two or more years back, are excluded.
  • card_type , transaction_date and amount are never NULL. A transaction whose card_id matches no row in cards has no card type and is excluded.
  • Two transactions with the same card, date and amount are separate rows with different transaction_id values, and both count.