Each Reader's Three Latest Loans
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_id | reader_name |
|---|---|
| 1 | Wendell |
| 2 | Bianca |
| 3 | Soraya |
loans
| loan_id | loaned_on | reader_id | fine |
|---|---|---|---|
| 1 | 2021-07-31 | 1 | 30 |
| 2 | 2021-07-30 | 2 | 40 |
| 3 | 2021-07-31 | 3 | 70 |
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