Compute the net settlement amount between each pair of users from shared expenses

Quick Overview

Write a SQL query that turns shared-expense records into one net debt per pair of users, reporting who owes whom and how much. It tests directed aggregation, cancelling mutual debts within a pair, excluding a payer's own share, and sorting with name tie-breakers.

Compute the net settlement amount between each pair of users from shared expenses

Company: Microsoft

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A shared-expense app stores each expense with the user who paid for it and splits its cost into shares. Each `expense_shares` row means that user owes `share_amount` to the expense's `paid_by` user. Compute the net settlement between each pair of users, netting out mutual debts, and return `debtor`, `creditor` (user names) and `amount`. Output only pairs with a nonzero net debt (an empty result if there are none). Order by `amount` descending, then `debtor` ascending, then `creditor` ascending. ```hint Direction of a debt Check the direction on a single expense before you net anything: the user on the share row owes, and the payer is owed. A sign slip here flips every row of the answer. ``` ### Input Table(s) Table: `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | User id. Primary key. | | user_name | VARCHAR(100) | Display name. Unique, NOT NULL. | Table: `expenses` | Column | Type | Meaning | |---|---|---| | expense_id | INT | Expense id. Primary key. | | paid_by | INT | `users.user_id` of the user who paid the whole expense. NOT NULL. | | total_amount | DECIMAL(10,2) | Total cost of the expense. NOT NULL. | Table: `expense_shares` | Column | Type | Meaning | |---|---|---| | expense_id | INT | The expense this share belongs to. References `expenses.expense_id`. | | user_id | INT | The user who owes this share. References `users.user_id`. | | share_amount | DECIMAL(10,2) | The amount this user owes the payer for this expense. NOT NULL, positive. | Primary key of `expense_shares`: `(expense_id, user_id)`. ### Output Contract - Columns, in this order: `debtor` (the `user_name` of the user who owes after netting), `creditor` (the `user_name` of the user who is owed), `amount` (the positive net amount). - One row per pair of users whose debts to each other do not cancel out. A pair never appears in both directions. - `amount` is rounded to 2 decimal places. - Sort by `amount` descending, then `debtor` ascending, then `creditor` ascending. Names are unique and each pair appears once, so this order is unique. ### Example `users` | user_id | user_name | |---|---| | 1 | Alice | | 2 | Bob | | 3 | Carol | | 4 | Dan | `expenses` | expense_id | paid_by | total_amount | |---|---|---| | 10 | 1 | 90.00 | | 11 | 2 | 40.00 | | 12 | 3 | 60.00 | | 13 | 4 | 25.50 | | 14 | 4 | 20.00 | `expense_shares` | expense_id | user_id | share_amount | |---|---|---| | 10 | 1 | 30.00 | | 10 | 2 | 30.00 | | 10 | 3 | 30.00 | | 11 | 1 | 20.00 | | 11 | 2 | 20.00 | | 12 | 1 | 30.00 | | 12 | 3 | 30.00 | | 13 | 2 | 12.75 | | 13 | 4 | 12.75 | | 14 | 3 | 10.00 | | 14 | 4 | 10.00 | Expected result | debtor | creditor | amount | |---|---|---| | Bob | Dan | 12.75 | | Bob | Alice | 10.00 | | Carol | Dan | 10.00 | Bob owes Alice 30.00 from expense 10 and Alice owes Bob 20.00 from expense 11, so they net to Bob owing Alice 10.00. Carol and Alice each owe the other 30.00 (expenses 10 and 12), which cancels, so that pair is left out. Bob owes Dan 12.75 and Carol owes Dan 10.00. The two 10.00 rows tie on `amount`, and Bob sorts before Carol as debtor. ### Constraints and Clarifications - At most 100 users and 10,000 expenses. - `share_amount` is positive, and the shares of an expense sum to its `total_amount`. - The payer also has a share row for their own share. That row is not a debt and never produces output. - Monetary values have at most 2 decimal places. - Netting happens within a pair only. If Alice owes Bob and Bob owes Carol, report both rows; do not route a debt through a third user. - A pair whose debts cancel exactly is left out. If every pair cancels, or there are no debts, return an empty result. - Every `paid_by` and every `expense_shares.user_id` exists in `users`. None of the columns used here contain NULLs. - Users who never pay or owe anything do not appear in the result.

Overview: Write a SQL query that turns shared-expense records into one net debt per pair of users, reporting who owes whom and how much. It tests directed aggregation, cancelling mutual debts within a pair, excluding a payer's own share, and sorting with name tie-breakers.

Microsoft logo
Microsoft
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A shared-expense app stores each expense with the user who paid for it and splits its cost into shares. Each expense_shares row means that user owes share_amount to the expense's paid_by user.

Compute the net settlement between each pair of users, netting out mutual debts, and return debtor, creditor (user names) and amount. Output only pairs with a nonzero net debt (an empty result if there are none). Order by amount descending, then debtor ascending, then creditor ascending.

Input Table(s)

Table: users

ColumnTypeMeaning
user_idINTUser id. Primary key.
user_nameVARCHAR(100)Display name. Unique, NOT NULL.

Table: expenses

ColumnTypeMeaning
expense_idINTExpense id. Primary key.
paid_byINTusers.user_id of the user who paid the whole expense. NOT NULL.
total_amountDECIMAL(10,2)Total cost of the expense. NOT NULL.

Table: expense_shares

ColumnTypeMeaning
expense_idINTThe expense this share belongs to. References expenses.expense_id.
user_idINTThe user who owes this share. References users.user_id.
share_amountDECIMAL(10,2)The amount this user owes the payer for this expense. NOT NULL, positive.

Primary key of expense_shares: (expense_id, user_id).

Output Contract

  • Columns, in this order: debtor (the user_name of the user who owes after netting), creditor (the user_name of the user who is owed), amount (the positive net amount).
  • One row per pair of users whose debts to each other do not cancel out. A pair never appears in both directions.
  • amount is rounded to 2 decimal places.
  • Sort by amount descending, then debtor ascending, then creditor ascending. Names are unique and each pair appears once, so this order is unique.

Example

users

user_iduser_name
1Alice
2Bob
3Carol
4Dan

expenses

expense_idpaid_bytotal_amount
10190.00
11240.00
12360.00
13425.50
14420.00

expense_shares

expense_iduser_idshare_amount
10130.00
10230.00
10330.00
11120.00
11220.00
12130.00
12330.00
13212.75
13412.75
14310.00
14410.00

Expected result

debtorcreditoramount
BobDan12.75
BobAlice10.00
CarolDan10.00

Bob owes Alice 30.00 from expense 10 and Alice owes Bob 20.00 from expense 11, so they net to Bob owing Alice 10.00. Carol and Alice each owe the other 30.00 (expenses 10 and 12), which cancels, so that pair is left out. Bob owes Dan 12.75 and Carol owes Dan 10.00. The two 10.00 rows tie on amount, and Bob sorts before Carol as debtor.

Constraints and Clarifications

  • At most 100 users and 10,000 expenses.
  • share_amount is positive, and the shares of an expense sum to its total_amount .
  • The payer also has a share row for their own share. That row is not a debt and never produces output.
  • Monetary values have at most 2 decimal places.
  • Netting happens within a pair only. If Alice owes Bob and Bob owes Carol, report both rows; do not route a debt through a third user.
  • A pair whose debts cancel exactly is left out. If every pair cancels, or there are no debts, return an empty result.
  • Every paid_by and every expense_shares.user_id exists in users . None of the columns used here contain NULLs.
  • Users who never pay or owe anything do not appear in the result.