Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Anomaly Detection with Aggregations

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

Your question is SQL Anomaly Detection with Aggregations. Start with the requirements and the three 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

Huntington's Data Analytics team is reviewing posted debit card activity for unusual daily transaction totals by branch. Write a PostgreSQL query to identify branch dates where the daily transaction amount is unusually high or low compared with that branch's observed daily average.

Requirements

  1. Join branches, accounts, and card_transactions using their keys, and include only posted transactions from January 2025.
  2. Aggregate transactions by branch and transaction date, returning transaction count and total amount.
  3. Calculate each branch's average observed daily amount using a CTE.
  4. Flag a day as high_amount when its total exceeds 150% of the branch average, or low_amount when it is below 50%. Return only anomalous days, ordered by branch and date.

Schema

branches
ColumnTypeDescription
branch_idPKINTHuntington branch identifier
branch_nameVARCHAR(100)Branch name
accounts
ColumnTypeDescription
account_idPKINTAccount identifier
branch_idINTBranch where the account was opened
card_transactions
ColumnTypeDescription
transaction_idPKINTTransaction identifier
account_idINTAccount associated with the transaction
transaction_dateDATETransaction date
amountDECIMAL(10,2)Transaction amount
transaction_statusVARCHAR(20)Transaction processing status
Tablesbranchesaccountscard_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results