Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 7-Day Customer Average

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rolling 7-Day Customer Average. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

IDFC FIRST Bank wants to monitor short-term customer transaction patterns. Write a PostgreSQL query that calculates the rolling 7-day average transaction amount for each active customer using a window function.

Requirements

  1. Include only active customers and transactions with transaction_status = 'POSTED'.
  2. Exclude transactions where transaction_amount is NULL.
  3. Calculate the average over the current transaction date and the preceding six calendar days, partitioned by customer.
  4. Return one row per qualifying transaction, ordered by customer_id, transaction_date, and transaction_id.

Schema

bank_customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer's name
is_activeBOOLEANWhether the customer has an active banking relationship
bank_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
customer_idINTCustomer who made the transaction
transaction_dateDATETransaction posting date
transaction_amountNUMERIC(12,2)Transaction amount
transaction_statusVARCHAR(20)Transaction processing status
Tablesbank_customersbank_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results