Skip to the content

What can I take home?

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

titleauthor
Maps of SaltErin Reilly
Three WintersTom Vance
Loose LeavesMartha Sloan
Long NightPatrick Lyle
Short Storiesno 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;
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 log n) challenges

See all challenges →