easy

Average Transaction Amount per Account

You are working as a Data Engineer for a FinTech company. The finance team wants to understand the average transaction amount per account to analyze customer spending behavior.

You are given a table called transactions with the following structure:

  • transaction_id (INTEGER): Unique identifier for each transaction

  • account_id (INTEGER): Identifier for the customer account

  • transaction_date (DATE): Date of the transaction

  • amount (NUMERIC): Transaction amount

Task:

Write a SQL query to calculate the average transaction amount for each account.

Expected Output:

  • account_id

  • avg_transaction_amount (average amount per account, rounded to 2 decimal places)

The result should be sorted by account_id in ascending order.

1
2
3
4
5
Preparing...
Average Transaction Amount per Account

You are working as a Data Engineer for a FinTech company. The finance team wants to understand the average transaction amount per account to analyze customer spending behavior. You are given a table called transactions with the following structure: transaction_id (INTEGER): Unique identifier for each transaction account_id (INTEGER): Identifier for the customer account transaction_date (DATE): Date of the transaction amount (NUMERIC): Transaction amount Task: Write a SQL query to calculate the average transaction amount for each account . Expected Output: account_id avg_transaction_amount (average amount per account, rounded to 2 decimal places) The result should be sorted by account_id in ascending order.

Table Setup (SQL Schema)

-- DDL: Create table
CREATE TABLE transactions (
    transaction_id INTEGER PRIMARY KEY,
    account_id INTEGER,
    transaction_date DATE,
    amount NUMERIC(10, 2)
);

-- DML: Insert sample data
INSERT INTO transactions (transaction_id, account_id, transaction_date, amount) VALUES
(1, 301, '2024-01-01', 100.00),
(2, 302, '2024-01-02', 200.00),
(3, 301, '2024-01-03', 150.00),
(4, 303, '2024-01-04', 300.00),
(5, 302, '2024-01-05', 250.00),
(6, 301, '2024-01-06', 50.00),
(7, 304, '2024-01-07', 400.00),
(8, 303, '2024-01-08', 200.00),
(9, 304, '2024-01-09', 100.00),
(10, 302, '2024-01-10', 150.00);
Home Videos Quiz Blog