Find users with more than 3 transactions in both 2024 and 2025

Quick Overview

Write a SQL query that returns the users who made more than three transactions in 2024 and also more than three in 2025. It tests grouping, conditional aggregation inside a HAVING clause, and applying two separate per-year thresholds to the same user.

Find users with more than 3 transactions in both 2024 and 2025

Company: Disney

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Return the `user_id` of users with strictly more than 3 transactions in 2024 and strictly more than 3 in 2025, sorted by `user_id` ascending. Users meeting the threshold in only one year are excluded. ```hint One pass Both yearly counts can come out of the same grouping of the table. Think about how to count only the rows that match a condition inside an aggregate. ``` ### Input Table(s) `Transactions` (primary key `transaction_id`) | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Unique id of the transaction (primary key) | | user_id | INT | User who made the transaction, NOT NULL | | transaction_date | DATE | Date of the transaction, NOT NULL | | amount | DECIMAL(10,2) | Transaction amount, non-negative, NOT NULL | ### Output Contract A single column, `user_id` (INT), with one row per qualifying user, sorted by `user_id` ascending. Each user appears at most once, so the order is unique. The result can be empty. ### Example `Transactions` | transaction_id | user_id | transaction_date | amount | |---|---|---|---| | 1 | 201 | 2024-01-01 | 10.00 | | 2 | 201 | 2024-02-01 | 10.00 | | 3 | 201 | 2024-03-01 | 10.00 | | 4 | 201 | 2024-04-01 | 10.00 | | 5 | 201 | 2025-01-01 | 10.00 | | 6 | 201 | 2025-02-01 | 10.00 | | 7 | 201 | 2025-03-01 | 10.00 | | 8 | 202 | 2024-01-15 | 25.00 | | 9 | 202 | 2024-05-20 | 40.00 | | 10 | 202 | 2024-08-02 | 15.00 | | 11 | 202 | 2024-12-31 | 30.00 | | 12 | 202 | 2025-01-01 | 20.00 | | 13 | 202 | 2025-03-14 | 35.00 | | 14 | 202 | 2025-06-30 | 10.00 | | 15 | 202 | 2025-11-11 | 50.00 | | 16 | 201 | 2026-01-05 | 10.00 | Expected result: | user_id | |---| | 202 | User 201 has 4 transactions in 2024 (more than 3) but only 3 in 2025 (not more than 3), so they do not qualify. Their transaction on 2026-01-05 belongs to neither year and does not help. User 202 has 4 transactions in each year and qualifies. ### Constraints and Clarifications - The table has between 1 and 100,000 rows. - `transaction_id` is the unique primary key, so every row is one distinct transaction. There are no duplicate rows to remove. - `user_id` is a positive integer and `amount` is non-negative. No column is NULL. - A year is the calendar year of `transaction_date`, from January 1 through December 31. - "Strictly more than 3" means at least 4 transactions in that year. - Each row counts as one transaction regardless of `amount`, including several transactions on the same day. - Transactions in years other than 2024 and 2025 are ignored.

Overview: Write a SQL query that returns the users who made more than three transactions in 2024 and also more than three in 2025. It tests grouping, conditional aggregation inside a HAVING clause, and applying two separate per-year thresholds to the same user.

Disney logo
Disney
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Return the user_id of users with strictly more than 3 transactions in 2024 and strictly more than 3 in 2025, sorted by user_id ascending. Users meeting the threshold in only one year are excluded.

Input Table(s)

Transactions (primary key transaction_id)

ColumnTypeMeaning
transaction_idINTUnique id of the transaction (primary key)
user_idINTUser who made the transaction, NOT NULL
transaction_dateDATEDate of the transaction, NOT NULL
amountDECIMAL(10,2)Transaction amount, non-negative, NOT NULL

Output Contract

A single column, user_id (INT), with one row per qualifying user, sorted by user_id ascending. Each user appears at most once, so the order is unique. The result can be empty.

Example

Transactions

transaction_iduser_idtransaction_dateamount
12012024-01-0110.00
22012024-02-0110.00
32012024-03-0110.00
42012024-04-0110.00
52012025-01-0110.00
62012025-02-0110.00
72012025-03-0110.00
82022024-01-1525.00
92022024-05-2040.00
102022024-08-0215.00
112022024-12-3130.00
122022025-01-0120.00
132022025-03-1435.00
142022025-06-3010.00
152022025-11-1150.00
162012026-01-0510.00

Expected result:

user_id
202

User 201 has 4 transactions in 2024 (more than 3) but only 3 in 2025 (not more than 3), so they do not qualify. Their transaction on 2026-01-05 belongs to neither year and does not help. User 202 has 4 transactions in each year and qualifies.

Constraints and Clarifications

  • The table has between 1 and 100,000 rows.
  • transaction_id is the unique primary key, so every row is one distinct transaction. There are no duplicate rows to remove.
  • user_id is a positive integer and amount is non-negative. No column is NULL.
  • A year is the calendar year of transaction_date , from January 1 through December 31.
  • "Strictly more than 3" means at least 4 transactions in that year.
  • Each row counts as one transaction regardless of amount , including several transactions on the same day.
  • Transactions in years other than 2024 and 2025 are ignored.