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 borrowed if there's a borrowing record with return_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
๐Ÿ“š BooksHarry Potter: 31984: 2Dune: 1๐Ÿ“‹ RecordsAlice โ†’ HPBob โ†’ HPCharlie โ†’ HPDavid โ†’ 1984(Unreturned)JOIN๐Ÿ”— CombinedHP: 3 total, 3 out1984: 2 total, 1 outDune: 1 total, 0 outFILTER๐ŸŽฏ ResultHarry Potter3/3 borrowedโŒ No copies left!๐Ÿ’ก Key Insight: Use HAVING COUNT(*) = total_copiesGROUP BY aggregates borrowers per book, HAVING filters completely borrowed books
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
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 25
52.0K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen