AnalystPath

Each Reader's Three Latest Loans

SQLMediumMid level~15 min

Problem

A library logs every book loan. For each reader, report their **three most recent loans** (by loan date, latest first). If a reader has fewer than three loans, return all of them.

Return `reader_name`, `reader_id`, `loan_id`, and `loaned_on`. Order the result by `reader_name`, then `reader_id`, then `loaned_on` descending.

Tables

Example rows — the live problem includes the full dataset.

readers
reader_idreader_name
1Wendell
2Bianca
3Soraya
loans
loan_idloaned_onreader_idfine
12021-07-31130
22021-07-30240
32021-07-31370

Expected output

Your answer should return 9 rows with the columns reader_name, reader_id, loan_id, loaned_on.

Starter code (SQL)

SELECT *
FROM readers;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions