From cf5e0f3285b8f1817e822c052e5a608bc691a580 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 4 Jun 2025 06:36:18 +0300 Subject: [PATCH 1/2] Added task 3570 --- .../readme.md | 106 ++++++++++++++++++ .../script.sql | 38 +++++++ .../MysqlTest.kt | 46 ++++---- .../MysqlTest.kt | 99 ++++++++++++++++ 4 files changed, 266 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/readme.md create mode 100644 src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/script.sql create mode 100644 src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt diff --git a/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/readme.md b/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/readme.md new file mode 100644 index 00000000..bcbc98bf --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/readme.md @@ -0,0 +1,106 @@ +3570\. Find Books with No Available Copies + +Easy + +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._ + +The result format is in the following example. + +**Example:** + +**Input:** + +library\_books table: + + +---------+--------------------------+----------------+-----------+------------------+--------------+ + | book_id | Title | Author | Genre | Publication Year | Total Copies | + |---------|--------------------------|----------------|-----------|------------------|--------------| + | 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | + | 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 | + | 3 | 1984 | George Orwell | Dystopian | 1949 | 1 | + | 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 | + | 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 | + | 6 | Brave New World | Aldous Huxley | Dystopian | 1932 | 4 | + +---------+--------------------------+----------------+-----------+------------------+--------------+ + +borrowing\_records table: + + +-----------+---------+---------------+-------------+-------------+ + | record_id | book_id | borrower_name | borrow_date | return_date | + |-----------|---------|---------------|-------------|-------------| + | 1 | 1 | Alice Smith | 2024-01-15 | NULL | + | 2 | 1 | Bob Johnson | 2024-01-20 | NULL | + | 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 | + | 4 | 3 | David Brown | 2024-02-01 | NULL | + | 5 | 4 | Emma Wilson | 2024-01-05 | NULL | + | 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 | + | 7 | 1 | Grace Miller | 2024-02-05 | NULL | + | 8 | 6 | Henry Taylor | 2024-01-12 | NULL | + | 9 | 2 | Ivan Clark | 2024-02-12 | NULL | + | 10 | 2 | Jane Adams | 2024-02-15 | NULL | + +-----------+---------+---------------+-------------+-------------+ + +**Output:** + + +---------+-------------------+----------------+-----------+------------------+-------------------+ + | book_id | Title | Author | Genre | Publication Year | Current Borrowers | + |---------|-------------------|----------------|-----------|------------------|-------------------| + | 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | + | 3 | 1984 | George Orwell | Dystopian | 1949 | 1 | + +---------+-------------------+----------------+-----------+------------------+-------------------+ + +**Explanation:** + +* **The Great Gatsby (book\_id = 1):** + * Total copies: 3 + * Currently borrowed by Alice Smith, Bob Johnson, and Grace Miller (3 borrowers) + * Available copies: 3 - 3 = 0 + * Included because available\_copies = 0 +* **1984 (book\_id = 3):** + * Total copies: 1 + * Currently borrowed by David Brown (1 borrower) + * Available copies: 1 - 1 = 0 + * Included because available\_copies = 0 +* **Books not included:** + * To Kill a Mockingbird (book\_id = 2): Total copies = 3, current borrowers = 2, available = 1 + * Pride and Prejudice (book\_id = 4): Total copies = 2, current borrowers = 1, available = 1 + * The Catcher in the Rye (book\_id = 5): Total copies = 1, current borrowers = 0, available = 1 + * Brave New World (book\_id = 6): Total copies = 4, current borrowers = 1, available = 3 +* **Result ordering:** + * The Great Gatsby appears first with 3 current borrowers + * 1984 appears second with 1 current borrower + +Output table is ordered by current\_borrowers in descending order, then by book\_title in ascending order. \ No newline at end of file diff --git a/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/script.sql b/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/script.sql new file mode 100644 index 00000000..db34b0ea --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/script.sql @@ -0,0 +1,38 @@ +# Write your MySQL query statement below +# #Easy #Database #2025_06_03_Time_512_ms_(100.00%)_Space_0.0_MB_(100.00%) +SELECT + book_id, + MAX(title) AS title, + MAX(author) AS author, + MAX(genre) AS genre, + MAX(publication_year) AS publication_year, + MAX(total_copies) AS current_borrowers +FROM ( + SELECT + book_id, + title, + author, + genre, + publication_year, + total_copies, + total_copies AS total_remain + FROM library_books + UNION ALL + SELECT + book_id, + '' AS title, + '' AS author, + '' AS genre, + 1000 AS publication_year, + 0 AS total_copies, + -1 AS total_remain + FROM borrowing_records + WHERE return_date IS NULL +) AS sub +GROUP BY + book_id +HAVING + SUM(total_remain) = 0 +ORDER BY + current_borrowers DESC, + title ASC; diff --git a/src/test/kotlin/g3501_3600/s3564_seasonal_sales_analysis/MysqlTest.kt b/src/test/kotlin/g3501_3600/s3564_seasonal_sales_analysis/MysqlTest.kt index 25d76d10..50d7585b 100644 --- a/src/test/kotlin/g3501_3600/s3564_seasonal_sales_analysis/MysqlTest.kt +++ b/src/test/kotlin/g3501_3600/s3564_seasonal_sales_analysis/MysqlTest.kt @@ -1,7 +1,7 @@ package g3501_3600.s3564_seasonal_sales_analysis -import org.hamcrest.CoreMatchers -import org.hamcrest.MatcherAssert +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat import org.junit.jupiter.api.Test import org.zapodot.junit.db.annotations.EmbeddedDatabase import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest @@ -66,27 +66,27 @@ internal class MysqlTest { .collect(Collectors.joining("\n")) .replace("#.*?\\r?\\n".toRegex(), ""), ).use { resultSet -> - MatcherAssert.assertThat(resultSet.next(), CoreMatchers.equalTo(true)) - MatcherAssert.assertThat(resultSet.getNString(1), CoreMatchers.equalTo("Fall")) - MatcherAssert.assertThat(resultSet.getNString(2), CoreMatchers.equalTo("Apparel")) - MatcherAssert.assertThat(resultSet.getNString(3), CoreMatchers.equalTo("10")) - MatcherAssert.assertThat(resultSet.getNString(4), CoreMatchers.equalTo("120")) - MatcherAssert.assertThat(resultSet.next(), CoreMatchers.equalTo(true)) - MatcherAssert.assertThat(resultSet.getNString(1), CoreMatchers.equalTo("Spring")) - MatcherAssert.assertThat(resultSet.getNString(2), CoreMatchers.equalTo("Kitchen")) - MatcherAssert.assertThat(resultSet.getNString(3), CoreMatchers.equalTo("3")) - MatcherAssert.assertThat(resultSet.getNString(4), CoreMatchers.equalTo("54")) - MatcherAssert.assertThat(resultSet.next(), CoreMatchers.equalTo(true)) - MatcherAssert.assertThat(resultSet.getNString(1), CoreMatchers.equalTo("Summer")) - MatcherAssert.assertThat(resultSet.getNString(2), CoreMatchers.equalTo("Tech")) - MatcherAssert.assertThat(resultSet.getNString(3), CoreMatchers.equalTo("5")) - MatcherAssert.assertThat(resultSet.getNString(4), CoreMatchers.equalTo("100")) - MatcherAssert.assertThat(resultSet.next(), CoreMatchers.equalTo(true)) - MatcherAssert.assertThat(resultSet.getNString(1), CoreMatchers.equalTo("Winter")) - MatcherAssert.assertThat(resultSet.getNString(2), CoreMatchers.equalTo("Apparel")) - MatcherAssert.assertThat(resultSet.getNString(3), CoreMatchers.equalTo("9")) - MatcherAssert.assertThat(resultSet.getNString(4), CoreMatchers.equalTo("110")) - MatcherAssert.assertThat(resultSet.next(), CoreMatchers.equalTo(false)) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("Fall")) + assertThat(resultSet.getNString(2), equalTo("Apparel")) + assertThat(resultSet.getNString(3), equalTo("10")) + assertThat(resultSet.getNString(4), equalTo("120")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("Spring")) + assertThat(resultSet.getNString(2), equalTo("Kitchen")) + assertThat(resultSet.getNString(3), equalTo("3")) + assertThat(resultSet.getNString(4), equalTo("54")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("Summer")) + assertThat(resultSet.getNString(2), equalTo("Tech")) + assertThat(resultSet.getNString(3), equalTo("5")) + assertThat(resultSet.getNString(4), equalTo("100")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("Winter")) + assertThat(resultSet.getNString(2), equalTo("Apparel")) + assertThat(resultSet.getNString(3), equalTo("9")) + assertThat(resultSet.getNString(4), equalTo("110")) + assertThat(resultSet.next(), equalTo(false)) } } } diff --git a/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt b/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt new file mode 100644 index 00000000..df55f25a --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt @@ -0,0 +1,99 @@ +package g3501_3600.s3570_find_books_with_no_available_copies + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test +import org.zapodot.junit.db.annotations.EmbeddedDatabase +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest +import org.zapodot.junit.db.common.CompatibilityMode +import java.io.BufferedReader +import java.io.FileNotFoundException +import java.io.FileReader +import java.sql.SQLException +import java.util.stream.Collectors +import javax.sql.DataSource + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = [ + ( + "CREATE TABLE library_books(book_id INTEGER, title VARCHAR(255)" + + ", author VARCHAR(255), genre VARCHAR(255), publication_year " + + "INTEGER, total_copies INTEGER); " + + "INSERT INTO library_books (book_id, title, author, genre, " + + "publication_year, total_copies) VALUES " + + "(1, 'The Great Gatsby', 'F. Scott', 'Fiction', 1925, 3)," + + "(2, 'To Kill a Mockingbird', 'Harper Lee', 'Fiction', 1960, 3)," + + "(3, '1984', 'George Orwell', 'Dystopian', 1949, 1)," + + "(4, 'Pride and Prejudice', 'Jane Austen', 'Romance', 1813, 2)," + + "(5, 'The Catcher in the Rye','J.D. Salinger', 'Fiction', 1951, 1)," + + "(6, 'Brave New World', 'Aldous Huxley', 'Dystopian', 1932, 4);" + + "CREATE TABLE borrowing_records(record_id INTEGER, book_id INTEGER" + + ", borrower_name VARCHAR(255), borrow_date DATE, return_date DATE); " + + "INSERT INTO borrowing_records(record_id, book_id, borrower_name, " + + "borrow_date, return_date) VALUES " + + "(1, 1, 'Alice Smith', '2024-01-15', NULL)," + + "(2, 1, 'Bob Johnson', '2024-01-20', NULL)," + + "(3, 2, 'Carol White', '2024-01-10', '2024-01-25')," + + "(4, 3, 'David Brown', '2024-02-01', NULL)," + + "(5, 4, 'Emma Wilson', '2024-01-05', NULL)," + + "(6, 5, 'Frank Davis', '2024-01-18', '2024-02-10')," + + "(7, 1, 'Grace Miller', '2024-02-05', NULL)," + + "(8, 6, 'Henry Taylor', '2024-01-12', NULL)," + + "(9, 2, 'Ivan Clark', '2024-02-12', NULL)," + + "(10,2, 'Jane Adams', '2024-02-15', NULL);" + ), + ], +) +internal class MysqlTest { + @Test + @Throws(SQLException::class, FileNotFoundException::class) + fun testScript(@EmbeddedDatabase dataSource: DataSource) { + dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery( + BufferedReader( + FileReader( + ( + "src/main/kotlin/g3501_3600/" + + "s3570_find_books_with_no_available_copies/" + + "script.sql" + ), + ), + ) + .lines() + .collect(Collectors.joining("\n")) + .replace("#.*\\r\\n".toRegex(), ""), + ).use { resultSet -> + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("1")) + assertThat( + resultSet.getNString(2), + equalTo("The Great Gatsby"), + ) + assertThat( + resultSet.getNString(3), + equalTo("F. Scott"), + ) + assertThat(resultSet.getNString(4), equalTo("Fiction")) + assertThat(resultSet.getNString(5), equalTo("1925")) + assertThat(resultSet.getNString(6), equalTo("3")) + assertThat(resultSet.next(), equalTo(true)) + assertThat(resultSet.getNString(1), equalTo("3")) + assertThat(resultSet.getNString(2), equalTo("1984")) + assertThat( + resultSet.getNString(3), + equalTo("George Orwell"), + ) + assertThat( + resultSet.getNString(4), + equalTo("Dystopian"), + ) + assertThat(resultSet.getNString(5), equalTo("1949")) + assertThat(resultSet.getNString(6), equalTo("1")) + assertThat(resultSet.next(), equalTo(false)) + } + } + } + } +} From 0bcbfadc374496598c67f060797d8f2f0b226765 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 4 Jun 2025 06:55:42 +0300 Subject: [PATCH 2/2] Fixed test --- .../s3570_find_books_with_no_available_copies/MysqlTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt b/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt index df55f25a..1a473ddc 100644 --- a/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt +++ b/src/test/kotlin/g3501_3600/s3570_find_books_with_no_available_copies/MysqlTest.kt @@ -63,7 +63,7 @@ internal class MysqlTest { ) .lines() .collect(Collectors.joining("\n")) - .replace("#.*\\r\\n".toRegex(), ""), + .replace("#.*?\\r?\\n".toRegex(), ""), ).use { resultSet -> assertThat(resultSet.next(), equalTo(true)) assertThat(resultSet.getNString(1), equalTo("1"))