Skip to content
Quantoros

Find potential duplicates: accountid, transactiondate, amount combinations appearing more than once, with a count.…

SQLLevel: IntermediateSubqueries and CTEs

Find potential duplicates: account_id, transaction_date, amount combinations appearing more than once, with a count.

Table definitions (from the source exercise):

CREATE TABLE transactions(transaction_id INTEGER PRIMARY KEY, account_id INTEGER, transaction_date TEXT, amount REAL, merchant_category TEXT);
Hints
  • Write an interleaving of two concurrent operations.
  • Which observations does the isolation level permit, and what happens on a mid-operation failure?

Answer

Approach: GROUP BY all three columns; HAVING COUNT(*) > 1.

SELECT account_id, transaction_date, amount, COUNT(*) AS occurrences
FROM transactions
GROUP BY account_id, transaction_date, amount
HAVING COUNT(*) > 1;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions