๐ฆ Loan Types Analysis
You're working as a data analyst for a financial institution that offers various loan products. Your task is to identify customers who have diversified their borrowing by taking both refinance loans and mortgage loans.
Given a Loans table with the following structure:
| Column Name | Type |
|---|---|
| loan_id | int |
| user_id | int |
| loan_type | varchar |
Your mission: Find all distinct user_ids that have at least one Refinance loan AND at least one Mortgage loan.
Output Requirements:
โข Return only the user_id column
โข Results must be ordered by user_id in ascending order
โข Each user should appear only once in the result
This is a classic set intersection problem in SQL - you need to find users who belong to both the 'Refinance' group AND the 'Mortgage' group!
Input & Output
Visualization
Time & Space Complexity
Single pass through the table with GROUP BY operation, where n is the number of loan records
Space proportional to number of distinct users (k), not total records (n)
Constraints
- 1 โค loan_id โค 105
- 1 โค user_id โค 104
- loan_type is a non-empty string with maximum length 50
- loan_id is unique (primary key)
- The table contains at least 1 row