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
Duplicate Records and Data Quality
00:00
5 left

Duplicate Records and Data Quality

MediumSQL · PostgreSQL

Problem

How do you handle duplicate records and ensure data quality before performing your analysis?

Use raw_records and record_sources to identify duplicate external record keys. Retain one quality-valid record per duplicate group, prioritizing valid data, then the most recently updated record.

Output

  1. One row per duplicate group with external_record_key, canonical_record_id, duplicate_count, and quality_issue_count
  2. Exclude groups with no quality-valid record
  3. Sort by external_record_key ascending

Output details

  • Treat a record as quality-valid only when customer_email is non-NULL and nonblank, event_date is non-NULL, amount is non-NULL and nonnegative, and its source is active; missing sources are inactive.
  • Count every record in each external-key group as duplicate_count, and count invalid records as quality_issue_count.
  • Require duplicate_count greater than 1 for a duplicate group.
  • After validity and descending updated_at, break selection ties with descending record_id; descending NULL timestamps sort first.
  • Retain only the selected record when it is quality-valid.

Schema

record_sources
ColumnTypeDescription
source_idPKINTUnique source identifier
source_nameVARCHAR(100)Name of the originating source
is_activeBOOLEANWhether the source is currently trusted
raw_records
ColumnTypeDescription
record_idPKINTUnique raw record identifier
external_record_keyVARCHAR(50)Business key used to identify duplicate records
source_idINTOriginating source identifier
customer_emailVARCHAR(255)Customer email address
event_dateDATEDate associated with the record
amountDECIMAL(10,2)Recorded amount
updated_atTIMESTAMPTimestamp of the latest source update
Tablesraw_recordsrecord_sources
Interviewer

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

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.