You are given two tables: library_books and borrowing_records.
The library_books table contains information about each book in the library, including the total number of copies owned. The borrowing_records table tracks all borrowing transactions, where return_date is NULL if the book is currently borrowed.
Write a SQL query to find all books that are currently borrowed (not returned) and have zero copies available in the library. This means all copies of the book are currently checked out.
Return the result ordered by:
- Current borrowers count in descending order
- Book title in ascending order
Table Schema
| Column Name | Type | Description |
|---|---|---|
book_id
PK
|
int | Unique identifier for each book |
title
|
varchar | Title of the book |
author
|
varchar | Author of the book |
genre
|
varchar | Genre category of the book |
publication_year
|
int | Year the book was published |
total_copies
|
int | Total number of copies owned by library |
| Column Name | Type | Description |
|---|---|---|
record_id
PK
|
int | Unique identifier for each borrowing record |
book_id
|
int | Foreign key referencing library_books.book_id |
borrower_name
|
varchar | Name of the person who borrowed the book |
borrow_date
|
date | Date when the book was borrowed |
return_date
|
date | Date when book was returned (NULL if still borrowed) |
Input & Output
| book_id | title | author | genre | publication_year | total_copies |
|---|---|---|---|---|---|
| 1 | SQL Guide | John Smith | Technology | 2020 | 2 |
| 2 | Python Basics | Jane Doe | Technology | 2019 | 1 |
| 3 | History 101 | Bob Wilson | History | 2018 | 3 |
| record_id | book_id | borrower_name | borrow_date | return_date |
|---|---|---|---|---|
| 1 | 1 | Alice | 2024-01-10 | |
| 2 | 1 | Bob | 2024-01-12 | |
| 3 | 2 | Charlie | 2024-01-15 | |
| 4 | 3 | David | 2024-01-08 | 2024-01-20 |
| 5 | 3 | Eve | 2024-01-18 |
| book_id | title | author | genre | publication_year | total_copies | current_borrowers |
|---|---|---|---|---|---|---|
| 1 | SQL Guide | John Smith | Technology | 2020 | 2 | 2 |
| 2 | Python Basics | Jane Doe | Technology | 2019 | 1 | 1 |
Book ID 1 'SQL Guide' has 2 total copies and 2 current borrowers (Alice and Bob with NULL return dates), so all copies are borrowed. Book ID 2 'Python Basics' has 1 total copy and 1 current borrower (Charlie), so it's also fully borrowed. Book ID 3 'History 101' has 3 copies but only 1 current borrower (Eve), so it's not included.
| book_id | title | author | genre | publication_year | total_copies |
|---|---|---|---|---|---|
| 1 | Math Theory | Dr. Johnson | Mathematics | 2021 | 3 |
| 2 | Art History | Sarah Lee | Arts | 2020 | 2 |
| record_id | book_id | borrower_name | borrow_date | return_date |
|---|---|---|---|---|
| 1 | 1 | Tom | 2024-01-10 | |
| 2 | 2 | Lisa | 2024-01-12 | 2024-01-25 |
| book_id | title | author | genre | publication_year | total_copies | current_borrowers |
|---|
No books are fully borrowed. 'Math Theory' has 3 copies but only 1 current borrower (Tom). 'Art History' has no current borrowers since Lisa already returned her copy.
Constraints
-
1 ≤ book_id ≤ 10000 -
1 ≤ record_id ≤ 100000 -
total_copies ≥ 1 -
return_dateisNULLif book is currently borrowed