Pages carried home
- O(n) · Medium
- Full plan
- SQL
- grouping
- joins
- decimals
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
| member | times | total | per_loan |
|---|---|---|---|
| Ann | 3 | 810 | 270 |
| Luke | 2 | 490 | 245 |
| Brian | 1 | 340 | 340 |
| Hannah | 1 | 260 | 260 |
| Dylan | 1 | 210 | 210 |
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.idIt 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
- Same book twice doesn't countSQL · grouping · sorting
- The 250 ClubSQL · grouping · joins · decimals
- The notebook by the registerSQL · grouping · joins
- What the team makesSQL · grouping · joins · decimals
- Where are the books from?SQL · grouping · joins · sorting
- Where do our orders come from?SQL · grouping · joins · nulls