Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Window Functions for Anomaly Patterns
00:00
5 left

Window Functions for Anomaly Patterns

MediumSQL · PostgreSQL

Problem

Write a SQL query using window functions to identify the top three repeating data anomalies by category over the last quarter for Macnica Americas. Use the provided anomaly event, category, and source tables. Treat the last quarter as the completed calendar quarter immediately before the current quarter. Include only anomaly codes occurring at least twice.

Output

  1. One row per category and repeating anomaly code.
  2. Columns: category_name, anomaly_code, anomaly_count, and anomaly_rank.
  3. Return the top three ranked anomaly codes within each category, allowing ties, ordered by category, rank, count descending, and anomaly code.

Schema

data_anomaly_events
ColumnTypeDescription
anomaly_idPKINTUnique anomaly event identifier
category_idINTReferenced anomaly category
source_idINTSource system that produced the event
anomaly_codeVARCHAR(80)Standardized anomaly code
detected_atTIMESTAMPTimestamp when the anomaly was detected
detailsVARCHAR(255)Optional event details
anomaly_categories
ColumnTypeDescription
category_idPKINTUnique category identifier
category_nameVARCHAR(100)Anomaly category name
data_sources
ColumnTypeDescription
source_idPKINTUnique source identifier
source_nameVARCHAR(120)Source system name
Tablesdata_anomaly_eventsanomaly_categoriesdata_sources
Interviewer

Your question is Window Functions for Anomaly Patterns. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.