cswork / libraryOpen practice workspace

Coding questions · LendingClub

Query Active Loans by Borrower ID in SQL

This question evaluates proficiency with SQL data aggregation, grouping, and ordering to compute borrower-level loan totals, assessing practical skills in relational data manipulation within the Data Manipulation (SQL/Python) domain.

loans

+---------+------------+----------+-----------+---------------+
| loan_id | borrower_id| amount   | status    | payment_date  |
+---------+------------+----------+-----------+---------------+
| 1001    | 501        | 5000.00  | ACTIVE    | 2024-01-15    |
| 1002    | 502        | 7000.00  | DEFAULTED | 2023-12-20    |
| 1003    | 501        | 3000.00  | ACTIVE    | 2024-02-10    |
| 1004    | 503        | 4500.00  | CLOSED    | 2023-11-01    |
| 1005    | 504        | 6500.00  | ACTIVE    | 2024-03-05    |
+---------+------------+----------+-----------+---------------+
Scenario

Live CodeShare SQL task for a LendingClub risk-analytics position; candidate must query loan data that feeds credit-decision systems.

Question

Using the loans table below, write a SQL query that returns each borrower_id and the total amount of all loans whose status is 'ACTIVE', ordered by borrower_id ascending.

Hints

Filter by status, GROUP BY borrower_id, SUM amount, ORDER BY borrower_id.

Total Active Loan Amount per Borrower

Table loans

columntypeconstraints
loan_idINTEGERPRIMARY KEY, NOT NULL
borrower_idINTEGERNOT NULL
amountDECIMAL(10,2)NOT NULL
statusVARCHAR(20)NOT NULL
payment_dateDATENOT NULL

Sample data:

amountborrower_idloan_idpayment_datestatus
500050110012024-01-15ACTIVE
700050210022023-12-20DEFAULTED
300050110032024-02-10ACTIVE
450050310042023-11-01CLOSED
650050410052024-03-05ACTIVE

Expected output

Each row lists a borrower_id and the sum of amounts for that borrower's loans where status = 'ACTIVE', ordered by borrower_id ascending.

borrower_idtotal_amount
5018000
5046500

Hints

  1. Filter by status = 'ACTIVE'.
  2. GROUP BY borrower_id.
  3. Use SUM(amount) AS total_amount.
  4. ORDER BY borrower_id ASC.