Skip to content

Average transactions per open account

MediumFull bankCTEs and subqueriesGroup and aggregateJoins12 min2 hidden datasets

The task

The product team wants to know how busy each kind of account is. Look only at open accounts (closed_on is NULL). An account that has never had a transaction is still an open account and counts as having 0 transactions.

For each account_type, return:

  • account_type
  • open_accounts: number of open accounts of that type
  • avg_txns: the average number of transactions per open account, rounded to 2 decimal places

Sort by account_type.

Expected output

account_typeopen_accountsavg_txns
  • Sorted by account_type ascending.
  • Numbers rounded to 2 decimal places.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Digital bank · click a name to insert it

customers70 rows in the sample

One row per customer. Key: customer_id.

intUnique id.
textName.
dateDay they became a customer.
textretail, premium or business.
accounts126 rows in the sample

One row per account. A customer can have several. Key: account_id.

intUnique id.
intOwner.
textchecking, savings or credit.
dateOpening day.
dateClosing day. NULL while open.
transactions9,906 rows in the sample

One row per posted transaction. Positive amounts are money in, negative are money out. Key: txn_id.

bigintUnique id.
intThe account.
timestampWhen it posted (UTC).
numeric(12,2)Signed amount, USD.
textsalary, rent, groceries, dining, travel, transfer, fees or refund.
textMerchant name. NULL for salary, transfers and fees.

Related questions

Loading the editor and PostgreSQL