Find Books with No Available Copies - Problem
๐ Library Management: Find Books with No Available Copies
You're helping a library manage their book inventory and borrowing system. The library has two main databases:
- library_books: Contains information about all books including total copies owned
- borrowing_records: Tracks when books are borrowed and returned
Your task is to find books that are in a critical situation: all copies are currently borrowed and none are available for new borrowers.
Key Rules:
- A book is
currently borrowedif there's a borrowing record withreturn_date = NULL - You need books where the number of unreturned copies equals the total copies
- Results should be ordered by current borrowers (descending), then by book title (ascending)
This helps librarians identify popular books that might need additional copies purchased!
Input & Output
example_1.sql โ Basic Case
$
Input:
library_books:
| book_id | title | author | genre | publication_year | total_copies |
|---------|-------|--------|-------|------------------|-------------|
| 1 | Harry Potter | J.K. Rowling | Fantasy | 1997 | 3 |
| 2 | 1984 | George Orwell | Dystopian | 1949 | 2 |
| 3 | Dune | Frank Herbert | Sci-Fi | 1965 | 1 |
borrowing_records:
| record_id | book_id | borrower_name | borrow_date | return_date |
|-----------|---------|---------------|-------------|-------------|
| 1 | 1 | Alice | 2024-01-01 | NULL |
| 2 | 1 | Bob | 2024-01-02 | NULL |
| 3 | 1 | Charlie | 2024-01-03 | NULL |
| 4 | 2 | David | 2024-01-04 | NULL |
| 5 | 3 | Eve | 2024-01-05 | 2024-01-10 |
โบ
Output:
| book_id | title | author | genre | publication_year | total_copies | current_borrowers |
|---------|-------|--------|-------|------------------|-------------|------------------|
| 1 | Harry Potter | J.K. Rowling | Fantasy | 1997 | 3 | 3 |
๐ก Note:
Harry Potter has 3 total copies and 3 current borrowers (Alice, Bob, Charlie), so no copies are available. 1984 has 2 total copies but only 1 current borrower. Dune has 0 current borrowers.
example_2.sql โ Multiple Sold Out Books
$
Input:
library_books:
| book_id | title | author | total_copies |
|---------|-------|--------|-------------|
| 1 | Book A | Author A | 2 |
| 2 | Book B | Author B | 1 |
| 3 | Book C | Author C | 3 |
borrowing_records:
| record_id | book_id | borrower_name | return_date |
|-----------|---------|---------------|-------------|
| 1 | 1 | User1 | NULL |
| 2 | 1 | User2 | NULL |
| 3 | 2 | User3 | NULL |
| 4 | 3 | User4 | NULL |
| 5 | 3 | User5 | NULL |
| 6 | 3 | User6 | NULL |
โบ
Output:
| book_id | title | author | total_copies | current_borrowers |
|---------|-------|--------|-------------|------------------|
| 3 | Book C | Author C | 3 | 3 |
| 1 | Book A | Author A | 2 | 2 |
| 2 | Book B | Author B | 1 | 1 |
๐ก Note:
All three books are completely sold out. Results are ordered by current_borrowers DESC (3, 2, 1), then by title ASC within same borrower count.
example_3.sql โ Edge Case: No Sold Out Books
$
Input:
library_books:
| book_id | title | author | total_copies |
|---------|-------|--------|-------------|
| 1 | Available Book | Author | 5 |
| 2 | Another Book | Author2 | 3 |
borrowing_records:
| record_id | book_id | borrower_name | return_date |
|-----------|---------|---------------|-------------|
| 1 | 1 | User1 | NULL |
| 2 | 1 | User2 | 2024-01-15 |
| 3 | 2 | User3 | NULL |
| 4 | 2 | User4 | NULL |
โบ
Output:
Empty result set
๐ก Note:
No books are completely sold out. Book 1 has 1/5 copies borrowed, Book 2 has 2/3 copies borrowed. Both have available copies.
Constraints
- 1 โค book_id โค 106
- 1 โค total_copies โค 100
- Book titles are unique within the library
- return_date is NULL for currently borrowed books
- Performance requirement: Solution should handle up to 105 books and 106 borrowing records efficiently
Visualization
Tap to expand
Understanding the Visualization
1
Join Data
Combine book information with current borrowing records
2
Filter Active
Keep only books that are currently borrowed (return_date IS NULL)
3
Group & Count
Group by book and count how many people currently have each book
4
Find Full Books
Keep only books where current borrowers = total copies (completely unavailable)
Key Takeaway
๐ฏ Key Insight: Use JOIN + GROUP BY + HAVING to efficiently find books where current borrower count equals total copies in a single database operation
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code