NovaPay wants to flag accounts with potentially fraudulent card activity. Write a SQL query to identify accounts that had at least 3 declined transactions within any 1-hour window on the same day.
Return only suspicious accounts and include the time window summary.
status = 'declined'.account_id and calendar date.account_id, customer_name, transaction_date, first_decline_time, last_decline_time, and declined_count.transaction_date ascending, then declined_count descending, then account_id.customers| column | type | description |
|---|---|---|
| customer_id | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer full name |
| risk_tier | VARCHAR(20) | Internal risk segment |
accounts| column | type | description |
|---|---|---|
| account_id | INT | Unique account identifier |
| customer_id | INT | References customer |
| account_status | VARCHAR(20) | Current account status |
| opened_at | DATE | Account open date |
transactions| column | type | description |
|---|---|---|
| transaction_id | INT | Unique transaction identifier |
| account_id | INT | References account |
| merchant_name | VARCHAR(100) | Merchant name |
| transaction_ts | TIMESTAMP | Transaction timestamp |
| amount | DECIMAL(10,2) | Transaction amount |
| status | VARCHAR(20) | approved, declined, or pending |
Representative rows are included below in the dataset.
| account_id | customer_name | transaction_date | first_decline_time | last_decline_time | declined_count |
|---|---|---|---|---|---|
| 101 | Alice Chen | 2024-06-01 | 2024-06-01 09:05:00 | 2024-06-01 09:50:00 | 3 |
| 104 | Diana Park | 2024-06-02 | 2024-06-02 14:00:00 | 2024-06-02 14:40:00 | 3 |
| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer full name |
| risk_tier | VARCHAR(20) | Internal fraud risk tier |
| Column | Type | Description |
|---|---|---|
| account_idPK | INT | Unique account identifier |
| customer_id | INT | References customers.customer_id |
| account_status | VARCHAR(20) | Current account status |
| opened_at | DATE | Date the account was opened |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| account_id | INT | References accounts.account_id |
| merchant_name | VARCHAR(100) | Merchant where the transaction occurred |
| transaction_ts | TIMESTAMP | Transaction timestamp |
| amount | DECIMAL(10,2) | Transaction amount |
| status | VARCHAR(20) | Transaction status |
| account_id | customer_id | account_status | opened_at |
|---|---|---|---|
| 101 | 1 | active | 2023-01-10 |
| 102 | 2 | active | 2023-03-15 |
| 103 | 3 | frozen | 2022-11-20 |
| 104 | 4 | active | 2024-01-05 |
| 105 | 5 | closed | 2021-09-09 |
| 106 | 6 | active | 2024-02-11 |
| 107 | 7 | active | 2023-07-19 |
| 108 | 8 | review | 2024-04-01 |
| 109 | 5 | reopened | 2023-08-24 |
| 110 | 2 | chargeback-watch | 2023-12-22 |
| 111 | 2 | closed | 2024-03-18 |
| 112 | 8 | investigating | 2023-02-10 |
| 113 | 7 | legacy | 2022-01-18 |
| 114 | 8 | limited | 2023-01-03 |
| 115 | 4 | closed | 2023-01-22 |
| 116 | 2 | restricted | 2023-06-21 |
| 117 | 5 | archived | 2023-06-03 |
| 118 | 3 | temporary-hold | 2024-01-09 |
| 119 | 6 | new | 2023-03-29 |
| 120 | 1 | dormant | 2022-06-03 |
| 121 | 2 | suspended | 2023-02-14 |
| 122 | 8 | closed | 2023-11-17 |
| 123 | 2 | active | 2021-09-28 |
| 124 | 6 | temporary-hold | 2024-03-11 |
| 125 | 1 | suspended | 2024-02-08 |
| customer_id | customer_name | risk_tier |
|---|---|---|
| 1 | Alice Chen | high |
| 2 | Brian Smith | medium |
| 3 | Carlos Rivera | low |
| 4 | Diana Park | high |
| 5 | Ethan Brooks | |
| 6 | Farah Khan | medium |
| 7 | Grace Lee | low |
| 8 | Hector Diaz | high |
| 9 | Maya Singh | low |
| 10 | Quentin Ross | low |
| 11 | Ivy Morgan | seasonal |
| 12 | Maya Singh | trusted |
| 13 | Noah Bennett | partner |
| 14 | Farah Khan | high |
| 15 | Brian Smith | employee |
| 16 | Tina Wu | legacy |
| 17 | Diana Park | low |
| 18 | Maya Singh | trusted |
| 19 | Grace Lee | elevated |
| 20 | Samuel Green | temporary |
| 21 | Ivy Morgan | seasonal |
| 22 | Samuel Green | seasonal |
| 23 | Alice Chen | null |
| 24 | Carlos Rivera | priority |
| 25 | Uma Shah | priority |
| 26 | Uma Shah | suspended |
| transaction_id | account_id | merchant_name | transaction_ts | amount | status |
|---|---|---|---|---|---|
| 1001 | 102 | QuickFuel | 2024-06-01 11:10:00 | 45.00 | declined |
| 1002 | 101 | ElectroHub | 2024-06-01 09:50:00 | 220.00 | declined |
| 1003 | 104 | TravelNow | 2024-06-02 14:40:00 | 310.00 | declined |
| 1004 | 101 | ElectroHub | 2024-06-01 09:05:00 | 120.00 | declined |
| 1005 | 103 | CafeBlue | 2024-06-01 08:00:00 | 12.50 | approved |
| 1006 | 104 | TravelNow | 2024-06-02 14:00:00 | 280.00 | declined |
| 1007 | 101 | ElectroHub | 2024-06-01 09:20:00 | 180.00 | declined |
| 1008 | 102 | QuickFuel | 2024-06-01 12:30:00 | 46.00 | declined |
| 1009 | 104 | TravelNow | 2024-06-02 15:30:00 | 330.00 | declined |
| 1010 | 106 | FreshMart | 2024-06-03 10:00:00 | 0.00 | pending |
| 1011 | 107 | 2024-06-03 16:00:00 | -15.00 | declined | |
| 1012 | 108 | LuxStore | 2024-06-03 18:10:00 | 500.00 | declined |
| 1013 | 108 | LuxStore | 2024-06-03 19:20:00 | 510.00 | declined |
| 1014 | Unknown | 2024-06-04 09:00:00 | 75.00 | declined | |
| 1015 | 105 | BookBarn | 2024-06-01 13:00:00 | 25.00 | declined |
| 1016 | 104 | TravelNow | 2024-06-02 14:20:00 | 295.00 | declined |
| 1017 | 102 | QuickFuel | 2024-06-01 11:50:00 | 44.00 | approved |
| 1018 | 101 | ElectroHub | 2024-06-01 11:30:00 | 210.00 | declined |
| 1019 | 103 | CafeBlue | 2024-06-01 08:40:00 | 13.00 | declined |
| 1020 | 103 | CafeBlue | 2024-06-01 09:50:00 | 14.00 | declined |
| 1021 | 108 | HomeNest | 2024-06-05 18:08:51 | -35 | expired |
| 1022 | 104 | PharmaPlus | 2024-06-01 15:18:19 | 160 | posted |
| 1023 | 102 | PrimeStation | 2024-05-29 13:11:31 | 509 | duplicate |
| 1024 | 108 | NorthStar Hotel | 2024-06-05 03:48:27 | 236 | retry |
| 1025 | 108 | TravelNow | 2024-06-04 01:05:29 | 58 | posted |
| 1026 | 103 | OceanTrips | 2024-05-30 02:02:51 | 409 | partial_refund |
| 1027 | 103 | TechSquare | 2024-06-05 19:22:53 | -69 | settled |
| 1028 | 104 | ElectroHub | 2024-06-02 07:56:08 | -63 | duplicate |
| 1029 | 104 | DailyBites | 2024-06-02 05:22:22 | 564 | refunded |
| 1030 | 104 | StylePoint | 2024-05-31 10:50:11 | 199 | posted |
| account_id | customer_name | transaction_date | first_decline_time | last_decline_time | declined_count |
|---|---|---|---|---|---|
| 101 | Alice Chen | 2024-06-01 | 2024-06-01 09:05:00 | 2024-06-01 09:50:00 | 3 |
| 104 | Diana Park | 2024-06-02 | 2024-06-02 14:00:00 | 2024-06-02 14:40:00 | 3 |