6 kyu

Most Borrowed Books and Their Last Borrowers

Description:

The library management system uses two main tables - books and loans. The books table contains details about the books, while the loans table contains details about the loans.

books:

  • book_id (integer) - The unique identifier for a book
  • title (string) - The title of the book
  • author (string) - The author of the book

loans:

  • loan_id (integer) - The unique identifier for a loan
  • book_id (integer) - The identifier for the book that was loaned, corresponds to book_id in the books table
  • borrower_name (string) - The name of the person who borrowed the book
  • return_date (date) - The date when the book was returned. If the book has not yet been returned, this field is null.

Your task is to write a SQL query that identifies which books have been borrowed the most from a library and who was the last person to borrow them. If there is a tie, return all tied books, sorted by book_id in ascending order.

The output should be a single row (or list of rows in the case of the tie) with row containing the following columns:

  • book_id (integer) - The unique identifier for a book.
  • title (string) - The title of the book.
  • last_borrower (string) - The name of the last person who borrowed the book.
  • loan_count (integer) - The total count of loans for the book.

The "last borrower" of a book is determined based on the return_date in the loans table. There are two possibilities for each loan record:

  • The return_date is null: In this case, the book has not been returned yet, and the borrower_name is considered the current borrower of the book.
  • The return_date is not null: In this case, the book has been returned, and the borrower_name is considered a previous borrower.

When multiple loan records exist for the same book, the following rules are applied:

  • If there is a loan record with a null return_date, this indicates the book is currently borrowed and the borrower_name from this record is chosen as the last borrower.
  • If there are no loan records with a null return_date, the one with the most recent return_date is chosen as the last borrower. This is the person who returned the book most recently.

GLHF!

Desired Output

The desired output should look like this:

book_idtitlelast_borrowerloan_count
18The Sun Also RisesXavier Durgan I5
20Bury My Heart at Wounded KneeMsgr. Tammara Batz5
...
SQL
Databases

Stats:

CreatedJul 25, 2023
PublishedJul 25, 2023
Warriors Trained241
Total Skips22
Total Code Submissions836
Total Times Completed96
SQL Completions96
Total Stars9
% of votes with a positive feedback rating93% of 22
Total "Very Satisfied" Votes19
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • bornForThis Avatar
  • dfhwze Avatar
Ad