What's still out goes first
- O(log n) · Easy
- Free
- SQL
- sorting
- nulls
Problem
The library's front desk wants the list of loans the way it would be read out loud: on top, the books that are still out, starting with the one that's been out the longest. Below them, the ones that came back, from the latest return to the earliest. If two came back the same day, the one that went out earlier goes first.
Show member, borrowed, and the return date, in that order. Where there's no return date, it should say Still out. To the database, that label is one more piece of text in the column, and when text is sorted, a letter comes after any digit.
What it has to return
These rows, in this order
| member | borrowed | came_back |
|---|---|---|
| Dylan | 2026-01-10 | Still out |
| Hannah | 2026-01-12 | Still out |
| Ann | 2026-01-13 | Still out |
| Luke | 2026-01-08 | 2026-01-15 |
| Ann | 2026-01-10 | 2026-01-14 |
| Ann | 2026-01-02 | 2026-01-09 |
| Luke | 2026-01-03 | 2026-01-05 |
| Brian | 2025-12-20 | 2026-01-04 |
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 member, borrowed, returned
FROM loans
ORDER BY returned DESC;It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.