Retail Banking Operations Path · Mission 8 of 30Medium

Primary banking relationship classification

Multi-CTE EXISTS with cohort + per-month threshold

The Brief

Sarah ReyesCustomer Insights Leadretail-ops

I'm sizing the primary-banking-relationship cohort for the 2026 Q1 review. Use this definition (anchor date 2025-12-01): (a) at least one ACH-In transaction with `counterparty_name LIKE '%PAYROLL%'` between 2025-10-02 and 2025-12-01 (the trailing 60-day window), AND (b) at least 5 debit transactions per month in each of September, October, and November 2025 (all three months must qualify). Return one row per qualifying customer — just `customer_id`. Sort ascending.

You'll practice

EXISTSRolling window

Tables & columns available

retail_transactionsfact12 columns
ColumnTypeKey
txn_idINTPK
account_idINTFK → retail_accounts
branch_idINTFK → retail_branches
post_dateTEXT
channelTEXT
transaction_typeTEXT
debit_creditTEXT
amountREAL
descriptionTEXT
counterparty_nameTEXT
is_cash_flagINT
is_reversalINT
retail_accountsdim13 columns
ColumnTypeKey
account_idINTPK
customer_idINTFK → retail_customers
product_idINTFK → retail_products
branch_idINTFK → retail_branches
open_dateTEXT
close_dateTEXT
statusTEXT
ownership_typeTEXT
interest_rateREAL
original_balanceREAL
credit_limitREAL
fico_at_origINT
maturity_dateTEXT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Two CTEs make this readable. CTE 1: pick out account_ids with at least one PAYROLL ACH-In in the trailing-60-day window. CTE 2: count debits per (account_id, month) and keep the months where the count is ≥ 5.

Hint 2

After the CTEs, an account qualifies if it appears in CTE 1 AND its month-set in CTE 2 covers all three months (Sep/Oct/Nov 2025). `HAVING COUNT(DISTINCT month) = 3` after grouping CTE 2 by account_id is the cleanest gate.

Hint 3

Final SELECT joins back to `retail_accounts` to swap account_id for customer_id, with DISTINCT (a customer with two qualifying accounts should only appear once). The output is a single column.