Given a Loans table with information about different loan types for users, find all distinct user IDs that have both a Refinance loan type and a Mortgage loan type.
Table: Loans
+-------------+---------+ | Column Name | Type | +-------------+---------+ | loan_id | int | | user_id | int | | loan_type | varchar | +-------------+---------+
loan_id is the primary key for this table. Each row represents a loan with its associated user and type.
Return the result table ordered by user_id in ascending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
loan_id
PK
|
int | Unique identifier for each loan |
user_id
|
int | Identifier for the user who has the loan |
loan_type
|
varchar | Type of loan (e.g., Refinance, Mortgage, Personal) |
Input & Output
| loan_id | user_id | loan_type |
|---|---|---|
| 1 | 1 | Refinance |
| 2 | 1 | Mortgage |
| 3 | 2 | Refinance |
| 4 | 3 | Mortgage |
| 5 | 3 | Refinance |
| 6 | 3 | Personal |
| user_id |
|---|
| 1 |
| 3 |
User 1 has both Refinance (loan_id=1) and Mortgage (loan_id=2) loans. User 3 has both Refinance (loan_id=5) and Mortgage (loan_id=4) loans. User 2 only has a Refinance loan, so they don't qualify.
| loan_id | user_id | loan_type |
|---|---|---|
| 1 | 1 | Refinance |
| 2 | 2 | Mortgage |
| 3 | 3 | Personal |
| user_id |
|---|
No user has both Refinance and Mortgage loan types. Each user has only one loan type, so the result is empty.
| loan_id | user_id | loan_type |
|---|---|---|
| 1 | 1 | Refinance |
| 2 | 1 | Refinance |
| 3 | 1 | Mortgage |
| 4 | 2 | Mortgage |
| 5 | 2 | Mortgage |
| user_id |
|---|
| 1 |
User 1 has multiple Refinance loans but only needs at least one of each type to qualify. User 2 has multiple Mortgage loans but no Refinance loan, so they don't qualify.
Constraints
-
1 ≤ loan_id ≤ 10000 -
1 ≤ user_id ≤ 1000 -
loan_typeis a valid loan type string