Find VIP loan users with more than three large loans in 2023
Quick Overview
Write a SQL query over a loans table that finds users with more than three loans above 20,000 dated in 2023 and returns each user's count of qualifying loans. It tests row filtering, grouping, filtering on an aggregate, strict thresholds, date ranges and deterministic ordering.
Find VIP loan users with more than three large loans in 2023
Company: SoFi
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find users with more than 3 qualifying loans. A loan qualifies when its `loan_amount` is greater than 20000 and its `loan_date` falls in calendar year 2023 (`2023-01-01` to `2023-12-31`). Return each such user's `user_id` and `loan_count`, the number of qualifying loans they have.
```hint Where each condition belongs
Some of the conditions are about a single loan and one is about a user's total. Decide which ones can be checked before grouping.
```
### Input Table(s)
Table `loans`, one row per loan. Primary key: `loan_id`. A user can have any number of loans.
| Column | Type | Meaning |
|---|---|---|
| loan_id | INT | Loan identifier (primary key) |
| user_id | INT | User who took the loan (NOT NULL) |
| loan_amount | DECIMAL(12,2) | Amount of the loan (NOT NULL, non-negative) |
| loan_date | DATE | Date of the loan (NOT NULL) |
### Output Contract
- Columns, in this order: `user_id`, `loan_count`.
- `loan_count` is an integer: the number of the user's loans with `loan_amount > 20000` and `loan_date` in 2023.
- One row per user whose `loan_count` is greater than 3. Users at 3 or fewer qualifying loans do not appear.
- Order by `loan_count` descending, then `user_id` ascending.
- No rounding is involved.
### Example
Input `loans`:
| loan_id | user_id | loan_amount | loan_date |
|---|---|---|---|
| 1 | 101 | 25000 | 2023-02-15 |
| 2 | 101 | 30000 | 2023-05-20 |
| 3 | 101 | 21000 | 2023-08-10 |
| 4 | 101 | 22500 | 2023-11-05 |
| 5 | 102 | 50000 | 2023-03-12 |
| 6 | 102 | 15000 | 2023-06-18 |
| 7 | 103 | 22000 | 2022-12-31 |
| 8 | 103 | 25000 | 2023-01-01 |
Expected output:
| user_id | loan_count |
|---|---|
| 101 | 4 |
All four of user 101's loans are above 20000 and dated in 2023, so the count is 4. User 102 has one qualifying loan (the 15000 loan is too small), and user 103 has one (the 2022-12-31 loan is outside the year), so neither passes the threshold.
### Constraints and Clarifications
- The table has 1 to 100,000 rows.
- Every column is NOT NULL, `loan_id` is unique and `loan_amount` is non-negative.
- Both thresholds are strict. A loan of exactly 20000 does not qualify, and a user with exactly 3 qualifying loans is not returned.
- Both ends of the date range are included: loans on `2023-01-01` and on `2023-12-31` count.
- If no user qualifies, return an empty result with the two columns.
Overview: Write a SQL query over a loans table that finds users with more than three loans above 20,000 dated in 2023 and returns each user's count of qualifying loans. It tests row filtering, grouping, filtering on an aggregate, strict thresholds, date ranges and deterministic ordering.
Find users with more than 3 qualifying loans. A loan qualifies when its loan_amount is greater than 20000 and its loan_date falls in calendar year 2023 (2023-01-01 to 2023-12-31). Return each such user's user_id and loan_count, the number of qualifying loans they have.
Input Table(s)
Table loans, one row per loan. Primary key: loan_id. A user can have any number of loans.
Column
Type
Meaning
loan_id
INT
Loan identifier (primary key)
user_id
INT
User who took the loan (NOT NULL)
loan_amount
DECIMAL(12,2)
Amount of the loan (NOT NULL, non-negative)
loan_date
DATE
Date of the loan (NOT NULL)
Output Contract
Columns, in this order:
user_id
,
loan_count
.
loan_count
is an integer: the number of the user's loans with
loan_amount > 20000
and
loan_date
in 2023.
One row per user whose
loan_count
is greater than 3. Users at 3 or fewer qualifying loans do not appear.
Order by
loan_count
descending, then
user_id
ascending.
No rounding is involved.
Example
Input loans:
loan_id
user_id
loan_amount
loan_date
1
101
25000
2023-02-15
2
101
30000
2023-05-20
3
101
21000
2023-08-10
4
101
22500
2023-11-05
5
102
50000
2023-03-12
6
102
15000
2023-06-18
7
103
22000
2022-12-31
8
103
25000
2023-01-01
Expected output:
user_id
loan_count
101
4
All four of user 101's loans are above 20000 and dated in 2023, so the count is 4. User 102 has one qualifying loan (the 15000 loan is too small), and user 103 has one (the 2022-12-31 loan is outside the year), so neither passes the threshold.
Constraints and Clarifications
The table has 1 to 100,000 rows.
Every column is NOT NULL,
loan_id
is unique and
loan_amount
is non-negative.
Both thresholds are strict. A loan of exactly 20000 does not qualify, and a user with exactly 3 qualifying loans is not returned.
Both ends of the date range are included: loans on
2023-01-01
and on
2023-12-31
count.
If no user qualifies, return an empty result with the two columns.