Find pairs of users who have paid each other
Company: PayPal
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each row of `payments` is one payment from `payer_id` to `recipient_id`. Find all unique pairs of users who have paid each other (A paid B and B paid A). Return each pair once with the smaller ID as `user1_id` and the larger as `user2_id`, ordered by `user1_id`, then `user2_id` ascending.
```hint Look for the reverse row
For a row where user A paid user B, ask which other row must exist somewhere in the table for the pair to count.
```
### Input Table(s)
`payments`
| Column | Type | Meaning |
|---|---|---|
| payment_id | INT, PRIMARY KEY | Unique ID of the payment |
| payer_id | INT, NOT NULL | User who sent the money |
| recipient_id | INT, NOT NULL | User who received the money |
| amount | DECIMAL(10,2), NOT NULL | Payment amount |
| payment_date | DATE, NOT NULL | Day the payment was made |
Key: `payment_id`. The same (`payer_id`, `recipient_id`) combination can appear in many rows.
### Output Contract
- Columns, in this order: `user1_id`, `user2_id`.
- One row per pair of users with at least one payment in each direction, with `user1_id < user2_id`.
- Order by `user1_id` ascending, then `user2_id` ascending. The pair is unique, so the order is fully determined.
- No rounding is involved; `amount` and `payment_date` do not affect the result.
### Example
Input `payments`:
| payment_id | payer_id | recipient_id | amount | payment_date |
|---|---|---|---|---|
| 1 | 1 | 2 | 50.00 | 2024-01-03 |
| 2 | 2 | 1 | 20.00 | 2024-01-05 |
| 3 | 1 | 2 | 15.00 | 2024-01-09 |
| 4 | 3 | 1 | 40.00 | 2024-01-10 |
| 5 | 4 | 3 | 10.00 | 2024-01-12 |
| 6 | 6 | 1 | 35.00 | 2024-01-14 |
| 7 | 3 | 4 | 25.00 | 2024-01-15 |
| 8 | 5 | 2 | 30.00 | 2024-01-18 |
| 9 | 1 | 6 | 12.50 | 2024-01-20 |
Expected output:
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| 1 | 6 |
| 3 | 4 |
Users 1 and 2 paid each other (user 1 did so twice, and the pair still appears once), users 1 and 6 paid each other, and users 3 and 4 paid each other. The last pair is written as 3, 4 even though user 4 paid first. User 3 paid user 1 and user 5 paid user 2, but neither payment was returned, so those pairs are left out.
### Constraints and Clarifications
- The table contains at least 1 row.
- `payer_id` and `recipient_id` are integers from 1 to 10,000.
- `payer_id` never equals `recipient_id`.
- Multiple payments may exist between the same pair, in either direction. Each pair appears once in the result.
- A pair counts as soon as one payment exists in each direction; amounts and dates do not matter.
- No column holds `NULL`.
- If no pair qualifies, return an empty result.
Overview: Write a SQL query that finds every pair of users who have sent payments to each other, returning each pair once with the smaller user ID first. It tests self-joins on reversed columns, collapsing repeated payments, and normalizing unordered pairs into a sorted result.