What can I take home?
- O(n log n) · Hard
- Full plan
- SQL
- subqueries
- nulls
- joins
Problem
Someone walks into the library today and asks what they can borrow. A book is on the shelf if none of its loans is still out: it doesn't matter how many times it has gone out before, and a book that has never been borrowed is on the shelf too.
Show the title of each available book and its author's name, in that order. The book with no author shows up anyway, with the text no author.
What it has to return
These rows, in any order
| title | author |
|---|---|
| Maps of Salt | Erin Reilly |
| Three Winters | Tom Vance |
| Loose Leaves | Martha Sloan |
| Long Night | Patrick Lyle |
| Short Stories | no author |
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 b.title
FROM books b
JOIN loans l
ON l.book_id = b.id
WHERE l.returned IS NOT NULL;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.