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
Finding Duplicates with Window Functions
00:00
5 left

Finding Duplicates with Window Functions

MediumSQL · PostgreSQL

Problem

Write a query utilizing window functions such as ROW_NUMBER or DENSE_RANK to identify duplicate records in a dataset.

Treat records as duplicates when all columns except record_id have identical values. Return every record belonging to a duplicate group, including its occurrence number and total duplicate count.

Output

  1. One row per record in a duplicate group.
  2. Return record_id, duplicate-defining columns, duplicate_sequence, and duplicate_count.
  3. Sort by the duplicate-defining columns, then record_id ascending.

Output details

  • Use duplicate-defining columns customer_email, event_type, event_date, and amount, in that output order.
  • Return columns in this order: record_id, customer_email, event_type, event_date, amount, duplicate_sequence, duplicate_count.
  • Assign duplicate_sequence by ascending record_id within each duplicate group.
  • Sort customer_email, event_type, event_date, and amount ascending before record_id.
  • Place NULL customer_email and amount values first when sorting.

Schema

dataset_records
ColumnTypeDescription
record_idPKINTUnique record identifier
customer_emailVARCHAR(255)Customer email associated with the record
event_typeVARCHAR(50)Type of recorded event
event_dateDATEDate when the event occurred
amountDECIMAL(10,2)Amount associated with the event
Tablesdataset_records
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table 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.