The emptiest shelf
- O(2ⁿ) · Legendary
- Full plan
- SQL
- window functions
- dates
- sets
Problem
The librarian looks at the empty spots on the shelf and wants the number: which day had the most books out at once, and how many were out? A book is out from the day it's borrowed until the day it comes back.
The count is taken at closing each day: a book that came back that day is already back on the shelf and doesn't count, and one that went out that day does. The ones that haven't come back are still out today, January 15, 2026.
Show a single row with that day and the number of books that were out, in that order. If the high was reached on more than one day, show the first of them.
What it has to return
These rows, in any order
| day | still_out |
|---|---|
| 2026-01-13 | 5 |
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 borrowed, returned
FROM loans
ORDER BY borrowedIt 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(2ⁿ) challenges
- The salary in the middleSQL · window functions · grouping
- The store on a streakSQL · window functions · dates
- The Winters oneSQL · recursion · strings · window functions
- Their people's peopleSQL · recursion · grouping
- Use it or lose itSQL · recursion · strings
- Can I borrow your notes?SQL · subqueries · joins · grouping