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
Start free
Diagnose Inconsistent Customer Report
00:00
5 left

Diagnose Inconsistent Customer Report

MediumSQL · PostgreSQL

Problem

A customer report in a customer data system suddenly looks inconsistent from one week to the next. You are asked to write a SQL query that surfaces the first places you would investigate: customers whose latest weekly snapshot has a different segment, a different active flag, or a different total interaction count than the prior snapshot. Return one row per customer for the most recent snapshot date, along with the prior snapshot date, the current and previous values, and a change_type label showing whether the inconsistency is driven by a segment change, status change, interaction count change, or no prior snapshot.

Use the latest two snapshots available for each customer. Include only customers whose latest snapshot exists in the data, and count interactions that occurred on or before each snapshot date.

Output details

  • Return columns in this order: customer_id, customer_name, latest_snapshot_date, previous_snapshot_date, current_segment, previous_segment, current_is_active, previous_is_active, current_total_interactions, previous_total_interactions, change_type.
  • Select snapshots by snapshot_date descending, breaking equal dates by snapshot_id descending.
  • Return only customers with a prior snapshot difference or no prior snapshot, excluding no_change rows.
  • Treat missing interaction totals as 0, and treat NULL segment or status values as distinct from non-NULL values.
  • When multiple differences exist, label them in priority order segment_changed, status_changed, then interaction_count_changed.
  • Order the final rows by customer_id ascending.

Schema

customer_profiles
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer full name
regionVARCHAR(50)Commercial region
customer_weekly_snapshot
ColumnTypeDescription
snapshot_idPKINTSnapshot row identifier
customer_idINTReferences customer_profiles.customer_id
snapshot_dateDATEWeekly snapshot date
segmentVARCHAR(50)Customer segment in AbbVie Customer 360
is_activeBOOLEANWhether the customer is marked active
customer_interactions
ColumnTypeDescription
interaction_idPKINTInteraction row identifier
customer_idINTReferences customer_profiles.customer_id
interaction_dateDATEDate of interaction
channelVARCHAR(50)Interaction channel
interaction_countINTNumber of interactions recorded
Tablescustomer_profilescustomer_weekly_snapshotcustomer_interactions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.