Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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
Experiences
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
Build my plan
SQL Duplicate Transactions Detection
00:00
5 left

SQL Duplicate Transactions Detection

MediumSQL · PostgreSQL

Problem

Write a SQL query to identify duplicate transactions within a specific time window across a distributed database.

For this exercise, treat transactions as duplicates when they share account, amount, currency, and transaction type, occur on different shards, and are no more than five minutes apart. Search from 2025-01-15 09:00:00+00 inclusive through 2025-01-15 10:00:00+00 exclusive.

Output

  1. One row per duplicate transaction pair.
  2. Return transaction_id_1, transaction_id_2, account_id, amount, occurred_at_1, occurred_at_2, shard_name_1, shard_name_2, and difference_seconds.
  3. Order by occurred_at_1, then transaction_id_1, then transaction_id_2.

Schema

transactions
ColumnTypeDescription
transaction_idPKBIGINTUnique transaction identifier
account_idBIGINTAccount associated with the transaction
amountNUMERIC(12,2)Transaction amount
currencyVARCHAR(3)ISO currency code
transaction_typeVARCHAR(30)Transaction classification
occurred_atTIMESTAMPTZTime the transaction occurred
shard_idVARCHAR(20)Distributed database shard containing the transaction
shards
ColumnTypeDescription
shard_idPKVARCHAR(20)Unique shard identifier
shard_nameVARCHAR(50)Human-readable shard name
regionVARCHAR(30)Deployment region for the shard
Tablestransactionsshards
Interviewer

Your question is SQL Duplicate Transactions Detection. Start with the requirements and the two 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.