cswork / libraryOpen practice workspace

Coding questions · Meta

Find top 3 books by total borrowed time

This question evaluates proficiency in data manipulation and aggregation using SQL or Python, covering joins, date arithmetic and null handling, grouping with distinct-count constraints, and top-N ordering with tie-breakers.

Using copies(copy_id, book_id) and checkouts(copy_id, checkout_date, return_date), compute for each book_id the total borrowed duration as the sum over all completed checkouts of DATEDIFF(day, checkout_date, return_date). Consider only rows where return_date IS NOT NULL when summing durations. Return the top 3 book_ids with at least 10 copies (COUNT(DISTINCT copy_id) > 10) ordered by total borrowed duration descending, breaking ties by book_id ascending.

Find Top 3 Books by Total Borrowed Time

You are given two tables in a library checkout system:

  • copies(copy_id, book_id) — each row is one physical copy of a book.
  • checkouts(copy_id, checkout_date, return_date) — each row is one checkout of a particular copy. return_date is NULL when the copy has not yet been returned.

Task. For each book_id, compute its total borrowed duration in days, defined as the sum, over all of that book's completed checkouts (rows where return_date IS NOT NULL), of (return_date - checkout_date). Ignore checkouts that have not been returned.

Then restrict to books that have more than 10 distinct copies in the copies table (i.e. COUNT(DISTINCT copy_id) > 10), and return the top 3 such books ranked by total borrowed duration.

Output. One row per selected book, with exactly these columns:

  • book_id
  • total_borrowed_days — the summed duration in days.

Order the result by total_borrowed_days descending, breaking ties by book_id ascending, and return at most 3 rows.

Table copies

columntypeconstraints
copy_idINTNOT NULL
book_idINTNOT NULL

Sample data:

book_idcopy_id
1011
1012
1013
1014
1015
1016
1017
1018
1019
10110
10111
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10334
10335
10336
10337
10338
10339
10340

Table checkouts

columntypeconstraints
copy_idINTNOT NULL
checkout_dateDATENOT NULL
return_dateDATE

Sample data:

checkout_datecopy_idreturn_date
2025-01-0112025-01-11
2025-02-0122025-02-06
2025-03-1032025-03-20
2025-04-0142025-04-04
2025-05-011NULL
2025-01-05122025-01-15
2025-02-10132025-02-25
2025-03-01142025-03-16
2025-04-0112NULL
2025-01-10232025-02-09
2025-03-01242025-03-31
2025-04-01252025-04-21
2025-05-1023NULL
2025-01-02342025-01-05
2025-02-01352025-02-02

Expected output

Top 3 books with more than 10 distinct copies, ranked by total borrowed days over completed checkouts (return_date NOT NULL), ordered by total_borrowed_days DESC then book_id ASC.

book_idtotal_borrowed_days
10480
10240
10128

Hints

  1. PostgreSQL has no DATEDIFF — subtract one DATE from another (return_date - checkout_date) to get the number of days as an integer.
  2. Find the eligible books first with a CTE that groups copies by book_id and keeps HAVING COUNT(DISTINCT copy_id) > 10, then sum durations only for those books.
  3. Use LIMIT 3 (not SELECT TOP 3) after an explicit ORDER BY total_borrowed_days DESC, book_id ASC to get a deterministic top-3.