Skip to the content

Pages carried home

Problem

The librarian wants to know who hauls the most: how many pages each member has carried home. A book that goes out twice gets carried twice: if someone borrows "The Lighthouse" a second time, its 210 pages count again. And it makes no difference whether the book is back or still out.

Show the member, how many loans they have, how many pages those add up to, and the average pages per loan, rounded to 1 decimal, in that order. Whoever has carried the most pages goes first; ties go alphabetically.

What it has to return

These rows, in this order

membertimestotalper_loan
Ann3810270
Luke2490245
Brian1340340
Hannah1260260
Dylan1210210

This is what comes out with the data in the practice database this challenge uses. The column names don’t count, so name yours however you like: what counts is how many there are and the order they come in. Besides this one, the challenge has hidden tests with other data that are revealed when you submit your solution.

You start with this

SQL

SELECT l.member, b.pages
FROM loans l
JOIN books b
  ON l.book_id = b.id
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →