-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3570. Find Books with No Available Copies.sql
More file actions
59 lines (53 loc) · 1.89 KB
/
3570. Find Books with No Available Copies.sql
File metadata and controls
59 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Question 3570. Find Books with No Available Copies
Link: https://leetcode.com/problems/find-books-with-no-available-copies/description/?envType=problem-list-v2&envId=database
Table: library_books
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| book_id | int |
| title | varchar |
| author | varchar |
| genre | varchar |
| publication_year | int |
| total_copies | int |
+------------------+---------+
book_id is the unique identifier for this table.
Each row contains information about a book in the library, including the total number of copies owned by the library.
Table: borrowing_records
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| record_id | int |
| book_id | int |
| borrower_name | varchar |
| borrow_date | date |
| return_date | date |
+---------------+---------+
record_id is the unique identifier for this table.
Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.
Write a solution to find all books that are currently borrowed (not returned) and have zero copies available in the library.
A book is considered currently borrowed if there exists a borrowing record with a NULL return_date
Return the result table ordered by current borrowers in descending order, then by book title in ascending order.
*/
WITH br AS (
SELECT
book_id,
COUNT(book_id) AS current_borrowers
FROM borrowing_records
WHERE return_date IS NULL
GROUP BY book_id
)
SELECT
lb.book_id,
lb.title,
lb.author,
lb.genre,
lb.publication_year,
br.current_borrowers
FROM library_books AS lb
INNER JOIN
br
ON lb.book_id = br.book_id
WHERE (lb.total_copies - br.current_borrowers) = 0
ORDER BY br.current_borrowers DESC, lb.title ASC