{ "data": { "question": { "questionId": "3910", "questionFrontendId": "3570", "categoryTitle": "Database", "boundTopicId": 3691093, "title": "Find Books with No Available Copies", "titleSlug": "find-books-with-no-available-copies", "content": "

Table: library_books

\n\n
\n+------------------+---------+\n| Column Name      | Type    |\n+------------------+---------+\n| book_id          | int     |\n| title            | varchar |\n| author           | varchar |\n| genre            | varchar |\n| publication_year | int     |\n| total_copies     | int     |\n+------------------+---------+\nbook_id is the unique identifier for this table.\nEach row contains information about a book in the library, including the total number of copies owned by the library.\n
\n\n

Table: borrowing_records

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| record_id     | int     |\n| book_id       | int     |\n| borrower_name | varchar |\n| borrow_date   | date    |\n| return_date   | date    |\n+---------------+---------+\nrecord_id is the unique identifier for this table.\nEach row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.\n
\n\n

Write a solution to find all books that are currently borrowed (not returned) and have zero copies available in the library.

\n\n\n\n

Return the result table ordered by current borrowers in descending order, then by book title in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

library_books table:

\n\n
\n+---------+------------------------+------------------+----------+------------------+--------------+\n| book_id | title                  | author           | genre    | publication_year | total_copies |\n+---------+------------------------+------------------+----------+------------------+--------------+\n| 1       | The Great Gatsby       | F. Scott         | Fiction  | 1925             | 3            |\n| 2       | To Kill a Mockingbird  | Harper Lee       | Fiction  | 1960             | 3            |\n| 3       | 1984                   | George Orwell    | Dystopian| 1949             | 1            |\n| 4       | Pride and Prejudice    | Jane Austen      | Romance  | 1813             | 2            |\n| 5       | The Catcher in the Rye | J.D. Salinger    | Fiction  | 1951             | 1            |\n| 6       | Brave New World        | Aldous Huxley    | Dystopian| 1932             | 4            |\n+---------+------------------------+------------------+----------+------------------+--------------+\n
\n\n

borrowing_records table:

\n\n
\n+-----------+---------+---------------+-------------+-------------+\n| record_id | book_id | borrower_name | borrow_date | return_date |\n+-----------+---------+---------------+-------------+-------------+\n| 1         | 1       | Alice Smith   | 2024-01-15  | NULL        |\n| 2         | 1       | Bob Johnson   | 2024-01-20  | NULL        |\n| 3         | 2       | Carol White   | 2024-01-10  | 2024-01-25  |\n| 4         | 3       | David Brown   | 2024-02-01  | NULL        |\n| 5         | 4       | Emma Wilson   | 2024-01-05  | NULL        |\n| 6         | 5       | Frank Davis   | 2024-01-18  | 2024-02-10  |\n| 7         | 1       | Grace Miller  | 2024-02-05  | NULL        |\n| 8         | 6       | Henry Taylor  | 2024-01-12  | NULL        |\n| 9         | 2       | Ivan Clark    | 2024-02-12  | NULL        |\n| 10        | 2       | Jane Adams    | 2024-02-15  | NULL        |\n+-----------+---------+---------------+-------------+-------------+\n
\n\n

Output:

\n\n
\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| book_id | title            | author        | genre     | publication_year | current_borrowers |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| 1       | The Great Gatsby | F. Scott      | Fiction   | 1925             | 3                 | \n| 3       | 1984             | George Orwell | Dystopian | 1949             | 1                 |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n
\n\n

Explanation:

\n\n\n\n

Output table is ordered by current_borrowers in descending order, then by book_title in ascending order.

\n
\n", "translatedTitle": "查找无可用副本的书籍", "translatedContent": "

表:library_books

\n\n
\n+------------------+---------+\n| Column Name      | Type    |\n+------------------+---------+\n| book_id          | int     |\n| title            | varchar |\n| author           | varchar |\n| genre            | varchar |\n| publication_year | int     |\n| total_copies     | int     |\n+------------------+---------+\nbook_id 是这张表的唯一主键。\n每一行包含图书馆中一本书的信息,包括图书馆拥有的副本总数。\n
\n\n

表:borrowing_records

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| record_id     | int     |\n| book_id       | int     |\n| borrower_name | varchar |\n| borrow_date   | date    |\n| return_date   | date    |\n+---------------+---------+\nrecord_id 是这张表的唯一主键。\n每一行代表一笔借阅交易并且如果这本书目前被借出并且还没有被归还,return_date 为 NULL。\n
\n\n

编写一个解决方案以找到 所有 当前被借出(未归还) 且图书馆中 无可用副本 的书籍。

\n\n\n\n

返回结果表按当前借阅者数量 降序 排列,然后按书名 升序 排列。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

library_books 表:

\n\n
\n+---------+------------------------+------------------+----------+------------------+--------------+\n| book_id | title                  | author           | genre    | publication_year | total_copies |\n+---------+------------------------+------------------+----------+------------------+--------------+\n| 1       | The Great Gatsby       | F. Scott         | Fiction  | 1925             | 3            |\n| 2       | To Kill a Mockingbird  | Harper Lee       | Fiction  | 1960             | 3            |\n| 3       | 1984                   | George Orwell    | Dystopian| 1949             | 1            |\n| 4       | Pride and Prejudice    | Jane Austen      | Romance  | 1813             | 2            |\n| 5       | The Catcher in the Rye | J.D. Salinger    | Fiction  | 1951             | 1            |\n| 6       | Brave New World        | Aldous Huxley    | Dystopian| 1932             | 4            |\n+---------+------------------------+------------------+----------+------------------+--------------+\n
\n\n

borrowing_records 表:

\n\n
\n+-----------+---------+---------------+-------------+-------------+\n| record_id | book_id | borrower_name | borrow_date | return_date |\n+-----------+---------+---------------+-------------+-------------+\n| 1         | 1       | Alice Smith   | 2024-01-15  | NULL        |\n| 2         | 1       | Bob Johnson   | 2024-01-20  | NULL        |\n| 3         | 2       | Carol White   | 2024-01-10  | 2024-01-25  |\n| 4         | 3       | David Brown   | 2024-02-01  | NULL        |\n| 5         | 4       | Emma Wilson   | 2024-01-05  | NULL        |\n| 6         | 5       | Frank Davis   | 2024-01-18  | 2024-02-10  |\n| 7         | 1       | Grace Miller  | 2024-02-05  | NULL        |\n| 8         | 6       | Henry Taylor  | 2024-01-12  | NULL        |\n| 9         | 2       | Ivan Clark    | 2024-02-12  | NULL        |\n| 10        | 2       | Jane Adams    | 2024-02-15  | NULL        |\n+-----------+---------+---------------+-------------+-------------+\n
\n\n

输出:

\n\n
\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| book_id | title            | author        | genre     | publication_year | current_borrowers |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| 1       | The Great Gatsby | F. Scott      | Fiction   | 1925             | 3                 | \n| 3       | 1984             | George Orwell | Dystopian | 1949             | 1                 |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n
\n\n

解释:

\n\n\n\n

输出表以 current_borrowers 降序排序,然后以 book_title 升序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_books_with_no_available_copies(library_books: pd.DataFrame, borrowing_records: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"383\", \"totalSubmission\": \"666\", \"totalAcceptedRaw\": 383, \"totalSubmissionRaw\": 666, \"acRate\": \"57.5%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"library_books\":[\"book_id\",\"title\",\"author\",\"genre\",\"publication_year\",\"total_copies\"],\"borrowing_records\":[\"record_id\",\"book_id\",\"borrower_name\",\"borrow_date\",\"return_date\"]},\"rows\":{\"library_books\":[[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\":[[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]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE library_books (\\n book_id INT,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n publication_year INT,\\n total_copies INT\\n)\",\"CREATE TABLE borrowing_records (\\n record_id INT,\\n book_id INT,\\n borrower_name VARCHAR(255),\\n borrow_date DATE,\\n return_date DATE\\n)\"],\"mssql\":[\"CREATE TABLE library_books (\\n book_id INT,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n publication_year INT,\\n total_copies INT\\n)\",\"CREATE TABLE borrowing_records (\\n record_id INT,\\n book_id INT,\\n borrower_name VARCHAR(255),\\n borrow_date DATE,\\n return_date DATE\\n)\"],\"oraclesql\":[\"CREATE TABLE library_books (\\n book_id NUMBER,\\n title VARCHAR2(255),\\n author VARCHAR2(255),\\n genre VARCHAR2(100),\\n publication_year NUMBER,\\n total_copies NUMBER\\n)\",\"CREATE TABLE borrowing_records (\\n record_id NUMBER,\\n book_id NUMBER,\\n borrower_name VARCHAR2(255),\\n borrow_date DATE,\\n return_date DATE\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_books_with_no_available_copies\",\"postgresql\":[\"CREATE TABLE library_books (\\n book_id INTEGER,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n publication_year INTEGER,\\n total_copies INTEGER\\n);\\n\",\"CREATE TABLE borrowing_records (\\n record_id INTEGER,\\n book_id INTEGER,\\n borrower_name VARCHAR(255),\\n borrow_date DATE,\\n return_date DATE\\n);\\n\"],\"pythondata\":[\"library_books = pd.DataFrame({\\n 'book_id': pd.Series(dtype='int'),\\n 'title': pd.Series(dtype='str'),\\n 'author': pd.Series(dtype='str'),\\n 'genre': pd.Series(dtype='str'),\\n 'publication_year': pd.Series(dtype='int'),\\n 'total_copies': pd.Series(dtype='int')\\n})\",\"borrowing_records = pd.DataFrame({\\n 'record_id': pd.Series(dtype='int'),\\n 'book_id': pd.Series(dtype='int'),\\n 'borrower_name': pd.Series(dtype='str'),\\n 'borrow_date': pd.Series(dtype='datetime64[ns]'),\\n 'return_date': pd.Series(dtype='datetime64[ns]')\\n})\"],\"database_schema\":{\"library_books\":{\"book_id\":\"INT\",\"title\":\"VARCHAR(255)\",\"author\":\"VARCHAR(255)\",\"genre\":\"VARCHAR(100)\",\"publication_year\":\"INT\",\"total_copies\":\"INT\"},\"borrowing_records\":{\"record_id\":\"INT\",\"book_id\":\"INT\",\"borrower_name\":\"VARCHAR(255)\",\"borrow_date\":\"DATE\",\"return_date\":\"DATE\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE library_books (\n book_id INT,\n title VARCHAR(255),\n author VARCHAR(255),\n genre VARCHAR(100),\n publication_year INT,\n total_copies INT\n)", "CREATE TABLE borrowing_records (\n record_id INT,\n book_id INT,\n borrower_name VARCHAR(255),\n borrow_date DATE,\n return_date DATE\n)", "Truncate table library_books", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('1', 'The Great Gatsby', 'F. Scott', 'Fiction', '1925', '3')", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('2', 'To Kill a Mockingbird', 'Harper Lee', 'Fiction', '1960', '3')", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('3', '1984', 'George Orwell', 'Dystopian', '1949', '1')", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('4', 'Pride and Prejudice', 'Jane Austen', 'Romance', '1813', '2')", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('5', 'The Catcher in the Rye', 'J.D. Salinger', 'Fiction', '1951', '1')", "insert into library_books (book_id, title, author, genre, publication_year, total_copies) values ('6', 'Brave New World', 'Aldous Huxley', 'Dystopian', '1932', '4')", "Truncate table borrowing_records", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('1', '1', 'Alice Smith', '2024-01-15', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('2', '1', 'Bob Johnson', '2024-01-20', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('3', '2', 'Carol White', '2024-01-10', '2024-01-25')", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('4', '3', 'David Brown', '2024-02-01', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('5', '4', 'Emma Wilson', '2024-01-05', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('6', '5', 'Frank Davis', '2024-01-18', '2024-02-10')", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('7', '1', 'Grace Miller', '2024-02-05', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('8', '6', 'Henry Taylor', '2024-01-12', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('9', '2', 'Ivan Clark', '2024-02-12', NULL)", "insert into borrowing_records (record_id, book_id, borrower_name, borrow_date, return_date) values ('10', '2', 'Jane Adams', '2024-02-15', NULL)" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"

PostgreSQL 16<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"library_books\":[\"book_id\",\"title\",\"author\",\"genre\",\"publication_year\",\"total_copies\"],\"borrowing_records\":[\"record_id\",\"book_id\",\"borrower_name\",\"borrow_date\",\"return_date\"]},\"rows\":{\"library_books\":[[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\":[[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]]}}", "__typename": "QuestionNode" } } }