Skip to content

Customers whose every account is closed

MediumFull bankGroup and aggregateRows with no matchNULL handling12 min2 hidden datasets

The task

The retention team wants a list of customers who have left the bank entirely. A customer has left when they have at least one account and every one of their accounts is closed. An account is closed when closed_on is not NULL.

Customers with no account at all have not left and are not in the list. A customer with even one open account is not in the list.

Return customer_id, full_name, accounts (how many accounts the customer has) and last_closed_on (the latest closed_on among their accounts).

Sort by customer_id ascending.

Expected output

customer_idfull_nameaccountslast_closed_on
  • Sorted by customer_id ascending.
  • 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