Compute account balances from a double-entry ledger in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A company keeps its books in a double-entry ledger. Every transaction posts at least one debit entry and one credit entry, and the debits and credits of a transaction add up to the same amount.
Compute each account's balance as the sum of its `DEBIT` amounts minus the sum of its `CREDIT` amounts. Return `account_id`, `account_name`, `account_type`, `balance`, ordered by `account_id` ascending. Accounts with no ledger entries are included with balance `0`.
```hint Edge case
Check what your query returns for an account that has no rows in `ledger_entries` at all.
```
### Input Table(s)
`accounts`
| Column | Type | Meaning |
|---|---|---|
| account_id | INT | Account identifier |
| account_name | VARCHAR(100) | Display name, for example `Cash` |
| account_type | VARCHAR(20) | Account class, for example `ASSET` or `REVENUE` |
Primary key: `account_id`.
`ledger_entries`
| Column | Type | Meaning |
|---|---|---|
| entry_id | INT | Entry identifier |
| transaction_id | INT | Transaction the entry belongs to; several entries share one transaction |
| account_id | INT | Account the entry posts to |
| entry_type | VARCHAR(10) | `'DEBIT'` or `'CREDIT'` |
| amount | DECIMAL(12,2) | Positive amount of the entry |
Primary key: `entry_id`. Foreign key: `account_id` references `accounts.account_id`.
### Output Contract
- One row per account in `accounts`, including accounts with no ledger entries.
- Columns, in this order: `account_id`, `account_name`, `account_type`, `balance`.
- `balance` is the sum of the account's `DEBIT` amounts minus the sum of its `CREDIT` amounts, as an exact decimal with two places. An account with no entries gets `0` (shown as `0.00`).
- Sort by `account_id` ascending. `account_id` is unique, so the order is fully determined.
### Example
`accounts`
| account_id | account_name | account_type |
|---|---|---|
| 1 | Cash | ASSET |
| 2 | Accounts Payable | LIABILITY |
| 3 | Sales Revenue | REVENUE |
| 4 | Office Supplies | EXPENSE |
| 5 | Prepaid Rent | ASSET |
`ledger_entries`
| entry_id | transaction_id | account_id | entry_type | amount |
|---|---|---|---|---|
| 1 | 101 | 1 | DEBIT | 500.00 |
| 2 | 101 | 3 | CREDIT | 500.00 |
| 3 | 102 | 4 | DEBIT | 120.00 |
| 4 | 102 | 2 | CREDIT | 120.00 |
| 5 | 103 | 2 | DEBIT | 120.00 |
| 6 | 103 | 1 | CREDIT | 120.00 |
Expected result:
| account_id | account_name | account_type | balance |
|---|---|---|---|
| 1 | Cash | ASSET | 380.00 |
| 2 | Accounts Payable | LIABILITY | 0.00 |
| 3 | Sales Revenue | REVENUE | -500.00 |
| 4 | Office Supplies | EXPENSE | 120.00 |
| 5 | Prepaid Rent | ASSET | 0.00 |
Cash has 500.00 of debits and 120.00 of credits, so its balance is 380.00. Accounts Payable has a debit and a credit of 120.00 each and nets to zero. Sales Revenue has only a credit, so its balance is negative. Prepaid Rent has no entries and still appears with 0.00. The five balances add up to zero, as they always do when every transaction balances.
### Constraints and Clarifications
- `entry_type` is `'DEBIT'` or `'CREDIT'`, in uppercase, and is never NULL.
- Every transaction has at least one `DEBIT` and one `CREDIT` entry with equal totals.
- `amount` is a positive `DECIMAL(12,2)`.
- At most 10,000 accounts and 100,000 ledger entries.
- Every `ledger_entries.account_id` exists in `accounts`.
- Use the same formula for every `account_type`. Liability, equity and revenue accounts normally carry credit balances, so their balances come out negative here; do not flip the sign.
- Each row of `ledger_entries` is a separate entry, so two entries with the same account, type and amount both count.
Overview: Write a SQL query that computes every account's balance from a double-entry ledger as total debits minus total credits, and still lists accounts that have no entries. It tests conditional aggregation, outer joins that keep unmatched rows, and turning an empty sum into zero.