Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Duplicate Transactions SQL
00:00
5 left

Duplicate Transactions SQL

EasySQL · PostgreSQL

Problem

Syfe's QA team needs to identify potentially duplicated entries in the user ledger before reconciliation. A transaction is considered duplicated when the same user has multiple ledger rows with the same transaction type, amount, currency, and timestamp. The transaction ID and processing status are not part of the duplicate definition.

Write a PostgreSQL query that returns every row belonging to a duplicate group and includes the user's email when available.

Requirements

  1. Use aggregation with GROUP BY and HAVING to identify duplicate transaction groups.
  2. Return the transaction ID, user ID, email, transaction attributes, and the number of rows in its duplicate group.
  3. Include ledger rows whose user record is unavailable, showing a NULL email.
  4. Order results by user_id, occurred_at, and transaction_id.

Schema

ledger_transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique ledger row identifier
user_idINTEGERSyfe user identifier
transaction_typeVARCHAR(30)Ledger event type
amountNUMERIC(12,2)Transaction amount
currencyVARCHAR(3)ISO currency code
occurred_atTIMESTAMPTime of the ledger event
processing_statusVARCHAR(20)Processing state of the ledger row
external_referenceVARCHAR(50)Reference from the upstream payment system
users
ColumnTypeDescription
user_idPKINTEGERSyfe user identifier
emailVARCHAR(120)User email address
Tablesledger_transactionsusers
Interviewer

Your question is Duplicate Transactions SQL. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.