Skip to content

Commit 96f4396

Browse files
authored
Added task 3570
1 parent d50384d commit 96f4396

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
3570\. Find Books with No Available Copies
2+
3+
Easy
4+
5+
Table: `library_books`
6+
7+
+------------------+---------+
8+
| Column Name | Type |
9+
+------------------+---------+
10+
| book_id | int |
11+
| title | varchar |
12+
| author | varchar |
13+
| genre | varchar |
14+
| publication_year | int |
15+
| total_copies | int |
16+
+------------------+---------+
17+
book_id is the unique identifier for this table.
18+
Each row contains information about a book in the library, including the total number of copies owned by the library.
19+
20+
Table: `borrowing_records`
21+
22+
+---------------+---------+
23+
| Column Name | Type |
24+
|----------------|---------|
25+
| record_id | int |
26+
| book_id | int |
27+
| borrower_name | varchar |
28+
| borrow_date | date |
29+
| return_date | date |
30+
+----------------+---------+
31+
record_id is the unique identifier for this table.
32+
Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.
33+
34+
Write a solution to find **all books** that are **currently borrowed (not returned)** and have **zero copies available** in the library.
35+
36+
* A book is considered **currently borrowed** if there exists a borrowing record with a **NULL** `return_date`
37+
38+
Return _the result table ordered by current borrowers in **descending** order, then by book title in **ascending** order._
39+
40+
The result format is in the following example.
41+
42+
**Example:**
43+
44+
**Input:**
45+
46+
library\_books table:
47+
48+
+---------+--------------------------+----------------+-----------+------------------+--------------+
49+
| book_id | Title | Author | Genre | Publication Year | Total Copies |
50+
|---------|--------------------------|----------------|-----------|------------------|--------------|
51+
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
52+
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 |
53+
| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |
54+
| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 |
55+
| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 |
56+
| 6 | Brave New World | Aldous Huxley | Dystopian | 1932 | 4 |
57+
+---------+--------------------------+----------------+-----------+------------------+--------------+
58+
59+
borrowing\_records table:
60+
61+
+-----------+---------+---------------+-------------+-------------+
62+
| record_id | book_id | borrower_name | borrow_date | return_date |
63+
|-----------|---------|---------------|-------------|-------------|
64+
| 1 | 1 | Alice Smith | 2024-01-15 | NULL |
65+
| 2 | 1 | Bob Johnson | 2024-01-20 | NULL |
66+
| 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 |
67+
| 4 | 3 | David Brown | 2024-02-01 | NULL |
68+
| 5 | 4 | Emma Wilson | 2024-01-05 | NULL |
69+
| 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 |
70+
| 7 | 1 | Grace Miller | 2024-02-05 | NULL |
71+
| 8 | 6 | Henry Taylor | 2024-01-12 | NULL |
72+
| 9 | 2 | Ivan Clark | 2024-02-12 | NULL |
73+
| 10 | 2 | Jane Adams | 2024-02-15 | NULL |
74+
+-----------+---------+---------------+-------------+-------------+
75+
76+
**Output:**
77+
78+
+---------+-------------------+----------------+-----------+------------------+-------------------+
79+
| book_id | Title | Author | Genre | Publication Year | Current Borrowers |
80+
|---------|-------------------|----------------|-----------|------------------|-------------------|
81+
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
82+
| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |
83+
+---------+-------------------+----------------+-----------+------------------+-------------------+
84+
85+
**Explanation:**
86+
87+
* **The Great Gatsby (book\_id = 1):**
88+
* Total copies: 3
89+
* Currently borrowed by Alice Smith, Bob Johnson, and Grace Miller (3 borrowers)
90+
* Available copies: 3 - 3 = 0
91+
* Included because available\_copies = 0
92+
* **1984 (book\_id = 3):**
93+
* Total copies: 1
94+
* Currently borrowed by David Brown (1 borrower)
95+
* Available copies: 1 - 1 = 0
96+
* Included because available\_copies = 0
97+
* **Books not included:**
98+
* To Kill a Mockingbird (book\_id = 2): Total copies = 3, current borrowers = 2, available = 1
99+
* Pride and Prejudice (book\_id = 4): Total copies = 2, current borrowers = 1, available = 1
100+
* The Catcher in the Rye (book\_id = 5): Total copies = 1, current borrowers = 0, available = 1
101+
* Brave New World (book\_id = 6): Total copies = 4, current borrowers = 1, available = 3
102+
* **Result ordering:**
103+
* The Great Gatsby appears first with 3 current borrowers
104+
* 1984 appears second with 1 current borrower
105+
106+
Output table is ordered by current\_borrowers in descending order, then by book\_title in ascending order.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2025_06_03_Time_512_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
SELECT
4+
book_id,
5+
MAX(title) AS title,
6+
MAX(author) AS author,
7+
MAX(genre) AS genre,
8+
MAX(publication_year) AS publication_year,
9+
MAX(total_copies) AS current_borrowers
10+
FROM (
11+
SELECT
12+
book_id,
13+
title,
14+
author,
15+
genre,
16+
publication_year,
17+
total_copies,
18+
total_copies AS total_remain
19+
FROM library_books
20+
UNION ALL
21+
SELECT
22+
book_id,
23+
'' AS title,
24+
'' AS author,
25+
'' AS genre,
26+
1000 AS publication_year,
27+
0 AS total_copies,
28+
-1 AS total_remain
29+
FROM borrowing_records
30+
WHERE return_date IS NULL
31+
) AS sub
32+
GROUP BY
33+
book_id
34+
HAVING
35+
SUM(total_remain) = 0
36+
ORDER BY
37+
current_borrowers DESC,
38+
title ASC;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package g3501_3600.s3570_find_books_with_no_available_copies;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE library_books(book_id INTEGER, title VARCHAR(255)"
24+
+ ", author VARCHAR(255), genre VARCHAR(255), publication_year "
25+
+ "INTEGER, total_copies INTEGER); "
26+
+ "INSERT INTO library_books (book_id, title, author, genre, "
27+
+ "publication_year, total_copies) VALUES "
28+
+ "(1, 'The Great Gatsby', 'F. Scott', 'Fiction', 1925, 3),"
29+
+ "(2, 'To Kill a Mockingbird', 'Harper Lee', 'Fiction', 1960, 3),"
30+
+ "(3, '1984', 'George Orwell', 'Dystopian', 1949, 1),"
31+
+ "(4, 'Pride and Prejudice', 'Jane Austen', 'Romance', 1813, 2),"
32+
+ "(5, 'The Catcher in the Rye','J.D. Salinger', 'Fiction', 1951, 1),"
33+
+ "(6, 'Brave New World', 'Aldous Huxley', 'Dystopian', 1932, 4);"
34+
+ "CREATE TABLE borrowing_records(record_id INTEGER, book_id INTEGER"
35+
+ ", borrower_name VARCHAR(255), borrow_date DATE, return_date DATE); "
36+
+ "INSERT INTO borrowing_records(record_id, book_id, borrower_name, "
37+
+ "borrow_date, return_date) VALUES "
38+
+ "(1, 1, 'Alice Smith', '2024-01-15', NULL),"
39+
+ "(2, 1, 'Bob Johnson', '2024-01-20', NULL),"
40+
+ "(3, 2, 'Carol White', '2024-01-10', '2024-01-25'),"
41+
+ "(4, 3, 'David Brown', '2024-02-01', NULL),"
42+
+ "(5, 4, 'Emma Wilson', '2024-01-05', NULL),"
43+
+ "(6, 5, 'Frank Davis', '2024-01-18', '2024-02-10'),"
44+
+ "(7, 1, 'Grace Miller', '2024-02-05', NULL),"
45+
+ "(8, 6, 'Henry Taylor', '2024-01-12', NULL),"
46+
+ "(9, 2, 'Ivan Clark', '2024-02-12', NULL),"
47+
+ "(10,2, 'Jane Adams', '2024-02-15', NULL);")
48+
class MysqlTest {
49+
@Test
50+
void testScript(@EmbeddedDatabase DataSource dataSource)
51+
throws SQLException, FileNotFoundException {
52+
try (final Connection connection = dataSource.getConnection()) {
53+
try (final Statement statement = connection.createStatement();
54+
final ResultSet resultSet =
55+
statement.executeQuery(
56+
new BufferedReader(
57+
new FileReader(
58+
"src/main/java/g3501_3600/"
59+
+ "s3570_find_books_with_no_available_copies/"
60+
+ "script.sql"))
61+
.lines()
62+
.collect(Collectors.joining("\n"))
63+
.replaceAll("#.*?\\r?\\n", ""))) {
64+
assertThat(resultSet.next(), equalTo(true));
65+
assertThat(resultSet.getNString(1), equalTo("1"));
66+
assertThat(resultSet.getNString(2), equalTo("The Great Gatsby"));
67+
assertThat(resultSet.getNString(3), equalTo("F. Scott"));
68+
assertThat(resultSet.getNString(4), equalTo("Fiction"));
69+
assertThat(resultSet.getNString(5), equalTo("1925"));
70+
assertThat(resultSet.getNString(6), equalTo("3"));
71+
assertThat(resultSet.next(), equalTo(true));
72+
assertThat(resultSet.getNString(1), equalTo("3"));
73+
assertThat(resultSet.getNString(2), equalTo("1984"));
74+
assertThat(resultSet.getNString(3), equalTo("George Orwell"));
75+
assertThat(resultSet.getNString(4), equalTo("Dystopian"));
76+
assertThat(resultSet.getNString(5), equalTo("1949"));
77+
assertThat(resultSet.getNString(6), equalTo("1"));
78+
assertThat(resultSet.next(), equalTo(false));
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)