SQL
Lab
Google Google Pay Interview Question 02

Fraud
Detection

Find users who made more than 3 transactions within 1 minute.

Table Schema

Inspect
Table

interactive
ColumnType
txn_idinteger
user_idinteger
amountdecimal
txn_timedatetime
device_idvarchar

Sample Data

Input
Output

Sample Input: transactions
txn_iduser_idamounttxn_timedevice_id
110110010:00:00A
210120010:00:20A
310115010:00:40A
410130010:00:50A
510250011:00:00B
610260011:05:00B
710320012:00:00C
Expected Output
user_id
101

SQL Editor

Run
Query

postgresql
Waiting for query

user_id
101

Hints

Unlock
Clues

Hint 01: Treat each transaction as a possible window start.
Hint 02: Join later transactions within 1 minute for the same user.
Hint 03: Keep windows where the count is greater than 3.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT DISTINCT
  t1.user_id
FROM transactions t1
JOIN transactions t2
  ON t1.user_id = t2.user_id
 AND t2.txn_time BETWEEN t1.txn_time AND t1.txn_time + INTERVAL '1 minute'
GROUP BY t1.user_id, t1.txn_id, t1.txn_time
HAVING COUNT(*) > 3;

Explanation

Step By
Step

01

Self-join transactions by user.

02

Limit the joined rows to a 1 minute window.

03

Return users with more than 3 transactions in any such window.

Related Questions

Keep
Solving