{ "data": { "question": { "questionId": "3996", "questionFrontendId": "3642", "categoryTitle": "Database", "boundTopicId": 3745957, "title": "Find Books with Polarized Opinions", "titleSlug": "find-books-with-polarized-opinions", "content": "

Table: books

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| book_id     | int     |\n| title       | varchar |\n| author      | varchar |\n| genre       | varchar |\n| pages       | int     |\n+-------------+---------+\nbook_id is the unique ID for this table.\nEach row contains information about a book including its genre and page count.\n
\n\n

Table: reading_sessions

\n\n
\n+----------------+---------+\n| Column Name    | Type    |\n+----------------+---------+\n| session_id     | int     |\n| book_id        | int     |\n| reader_name    | varchar |\n| pages_read     | int     |\n| session_rating | int     |\n+----------------+---------+\nsession_id is the unique ID for this table.\nEach row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5.\n
\n\n

Write a solution to find books that have polarized opinions - books that receive both very high ratings and very low ratings from different readers.

\n\n\n\n

Return the result table ordered by polarization score in descending order, then by title in descending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

books table:

\n\n
\n+---------+------------------------+---------------+----------+-------+\n| book_id | title                  | author        | genre    | pages |\n+---------+------------------------+---------------+----------+-------+\n| 1       | The Great Gatsby       | F. Scott      | Fiction  | 180   |\n| 2       | To Kill a Mockingbird  | Harper Lee    | Fiction  | 281   |\n| 3       | 1984                   | George Orwell | Dystopian| 328   |\n| 4       | Pride and Prejudice    | Jane Austen   | Romance  | 432   |\n| 5       | The Catcher in the Rye | J.D. Salinger | Fiction  | 277   |\n+---------+------------------------+---------------+----------+-------+\n
\n\n

reading_sessions table:

\n\n
\n+------------+---------+-------------+------------+----------------+\n| session_id | book_id | reader_name | pages_read | session_rating |\n+------------+---------+-------------+------------+----------------+\n| 1          | 1       | Alice       | 50         | 5              |\n| 2          | 1       | Bob         | 60         | 1              |\n| 3          | 1       | Carol       | 40         | 4              |\n| 4          | 1       | David       | 30         | 2              |\n| 5          | 1       | Emma        | 45         | 5              |\n| 6          | 2       | Frank       | 80         | 4              |\n| 7          | 2       | Grace       | 70         | 4              |\n| 8          | 2       | Henry       | 90         | 5              |\n| 9          | 2       | Ivy         | 60         | 4              |\n| 10         | 2       | Jack        | 75         | 4              |\n| 11         | 3       | Kate        | 100        | 2              |\n| 12         | 3       | Liam        | 120        | 1              |\n| 13         | 3       | Mia         | 80         | 2              |\n| 14         | 3       | Noah        | 90         | 1              |\n| 15         | 3       | Olivia      | 110        | 4              |\n| 16         | 3       | Paul        | 95         | 5              |\n| 17         | 4       | Quinn       | 150        | 3              |\n| 18         | 4       | Ruby        | 140        | 3              |\n| 19         | 5       | Sam         | 80         | 1              |\n| 20         | 5       | Tara        | 70         | 2              |\n+------------+---------+-------------+------------+----------------+\n
\n\n

Output:

\n\n
\n+---------+------------------+---------------+-----------+-------+---------------+--------------------+\n| book_id | title            | author        | genre     | pages | rating_spread | polarization_score |\n+---------+------------------+---------------+-----------+-------+---------------+--------------------+\n| 1       | The Great Gatsby | F. Scott      | Fiction   | 180   | 4             | 1.00               |\n| 3       | 1984             | George Orwell | Dystopian | 328   | 4             | 1.00               |\n+---------+------------------+---------------+-----------+-------+---------------+--------------------+\n
\n\n

Explanation:

\n\n\n\n

The result table is ordered by polarization score in descending order, then by book title in descending order.

\n
\n", "translatedTitle": null, "translatedContent": null, "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": [], "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_polarized_books(books: pd.DataFrame, reading_sessions: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"73\", \"totalSubmission\": \"115\", \"totalAcceptedRaw\": 73, \"totalSubmissionRaw\": 115, \"acRate\": \"63.5%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"books\":[\"book_id\",\"title\",\"author\",\"genre\",\"pages\"],\"reading_sessions\":[\"session_id\",\"book_id\",\"reader_name\",\"pages_read\",\"session_rating\"]},\"rows\":{\"books\":[[1,\"The Great Gatsby\",\"F. Scott\",\"Fiction\",180],[2,\"To Kill a Mockingbird\",\"Harper Lee\",\"Fiction\",281],[3,\"1984\",\"George Orwell\",\"Dystopian\",328],[4,\"Pride and Prejudice\",\"Jane Austen\",\"Romance\",432],[5,\"The Catcher in the Rye\",\"J.D. Salinger\",\"Fiction\",277]],\"reading_sessions\":[[1,1,\"Alice\",50,5],[2,1,\"Bob\",60,1],[3,1,\"Carol\",40,4],[4,1,\"David\",30,2],[5,1,\"Emma\",45,5],[6,2,\"Frank\",80,4],[7,2,\"Grace\",70,4],[8,2,\"Henry\",90,5],[9,2,\"Ivy\",60,4],[10,2,\"Jack\",75,4],[11,3,\"Kate\",100,2],[12,3,\"Liam\",120,1],[13,3,\"Mia\",80,2],[14,3,\"Noah\",90,1],[15,3,\"Olivia\",110,4],[16,3,\"Paul\",95,5],[17,4,\"Quinn\",150,3],[18,4,\"Ruby\",140,3],[19,5,\"Sam\",80,1],[20,5,\"Tara\",70,2]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE books (\\n book_id INT,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n pages INT\\n)\",\"CREATE TABLE reading_sessions (\\n session_id INT,\\n book_id INT,\\n reader_name VARCHAR(255),\\n pages_read INT,\\n session_rating INT\\n)\"],\"mssql\":[\"CREATE TABLE books (\\n book_id INT,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n pages INT\\n)\",\"CREATE TABLE reading_sessions (\\n session_id INT,\\n book_id INT,\\n reader_name VARCHAR(255),\\n pages_read INT,\\n session_rating INT\\n)\"],\"oraclesql\":[\"CREATE TABLE books (\\n book_id NUMBER,\\n title VARCHAR2(255),\\n author VARCHAR2(255),\\n genre VARCHAR2(100),\\n pages NUMBER\\n)\",\"CREATE TABLE reading_sessions (\\n session_id NUMBER,\\n book_id NUMBER,\\n reader_name VARCHAR2(255),\\n pages_read NUMBER,\\n session_rating NUMBER\\n)\"],\"database\":true,\"name\":\"find_polarized_books\",\"postgresql\":[\"CREATE TABLE books (\\n book_id SERIAL PRIMARY KEY,\\n title VARCHAR(255),\\n author VARCHAR(255),\\n genre VARCHAR(100),\\n pages INTEGER\\n);\\n\",\"CREATE TABLE reading_sessions (\\n session_id SERIAL PRIMARY KEY,\\n book_id INTEGER,\\n reader_name VARCHAR(255),\\n pages_read INTEGER,\\n session_rating INTEGER\\n);\\n\"],\"pythondata\":[\"books = pd.DataFrame({\\n \\\"book_id\\\": pd.Series(dtype=\\\"int64\\\"), # SERIAL -> int64\\n \\\"title\\\": pd.Series(dtype=\\\"string\\\"), # VARCHAR -> string dtype\\n \\\"author\\\": pd.Series(dtype=\\\"string\\\"), # VARCHAR -> string dtype\\n \\\"genre\\\": pd.Series(dtype=\\\"string\\\"), # VARCHAR -> string dtype\\n \\\"pages\\\": pd.Series(dtype=\\\"int64\\\") # INTEGER -> int64\\n})\",\"reading_sessions = pd.DataFrame({\\n \\\"session_id\\\": pd.Series(dtype=\\\"int64\\\"), # NUMBER -> int64\\n \\\"book_id\\\": pd.Series(dtype=\\\"int64\\\"), # NUMBER -> int64\\n \\\"reader_name\\\": pd.Series(dtype=\\\"string\\\"), # VARCHAR2 -> string dtype\\n \\\"pages_read\\\": pd.Series(dtype=\\\"int64\\\"), # NUMBER -> int64\\n \\\"session_rating\\\": pd.Series(dtype=\\\"int64\\\") # NUMBER -> int64\\n})\"],\"database_schema\":{\"books\":{\"book_id\":\"INT\",\"title\":\"VARCHAR(255)\",\"author\":\"VARCHAR(255)\",\"genre\":\"VARCHAR(100)\",\"pages\":\"INT\"},\"reading_sessions\":{\"session_id\":\"INT\",\"book_id\":\"INT\",\"reader_name\":\"VARCHAR(255)\",\"pages_read\":\"INT\",\"session_rating\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE books (\n book_id INT,\n title VARCHAR(255),\n author VARCHAR(255),\n genre VARCHAR(100),\n pages INT\n)", "CREATE TABLE reading_sessions (\n session_id INT,\n book_id INT,\n reader_name VARCHAR(255),\n pages_read INT,\n session_rating INT\n)", "Truncate table books", "insert into books (book_id, title, author, genre, pages) values ('1', 'The Great Gatsby', 'F. Scott', 'Fiction', '180')", "insert into books (book_id, title, author, genre, pages) values ('2', 'To Kill a Mockingbird', 'Harper Lee', 'Fiction', '281')", "insert into books (book_id, title, author, genre, pages) values ('3', '1984', 'George Orwell', 'Dystopian', '328')", "insert into books (book_id, title, author, genre, pages) values ('4', 'Pride and Prejudice', 'Jane Austen', 'Romance', '432')", "insert into books (book_id, title, author, genre, pages) values ('5', 'The Catcher in the Rye', 'J.D. Salinger', 'Fiction', '277')", "Truncate table reading_sessions", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('1', '1', 'Alice', '50', '5')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('2', '1', 'Bob', '60', '1')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('3', '1', 'Carol', '40', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('4', '1', 'David', '30', '2')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('5', '1', 'Emma', '45', '5')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('6', '2', 'Frank', '80', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('7', '2', 'Grace', '70', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('8', '2', 'Henry', '90', '5')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('9', '2', 'Ivy', '60', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('10', '2', 'Jack', '75', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('11', '3', 'Kate', '100', '2')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('12', '3', 'Liam', '120', '1')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('13', '3', 'Mia', '80', '2')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('14', '3', 'Noah', '90', '1')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('15', '3', 'Olivia', '110', '4')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('16', '3', 'Paul', '95', '5')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('17', '4', 'Quinn', '150', '3')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('18', '4', 'Ruby', '140', '3')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('19', '5', 'Sam', '80', '1')", "insert into reading_sessions (session_id, book_id, reader_name, pages_read, session_rating) values ('20', '5', 'Tara', '70', '2')" ], "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\":{\"books\":[\"book_id\",\"title\",\"author\",\"genre\",\"pages\"],\"reading_sessions\":[\"session_id\",\"book_id\",\"reader_name\",\"pages_read\",\"session_rating\"]},\"rows\":{\"books\":[[1,\"The Great Gatsby\",\"F. Scott\",\"Fiction\",180],[2,\"To Kill a Mockingbird\",\"Harper Lee\",\"Fiction\",281],[3,\"1984\",\"George Orwell\",\"Dystopian\",328],[4,\"Pride and Prejudice\",\"Jane Austen\",\"Romance\",432],[5,\"The Catcher in the Rye\",\"J.D. Salinger\",\"Fiction\",277]],\"reading_sessions\":[[1,1,\"Alice\",50,5],[2,1,\"Bob\",60,1],[3,1,\"Carol\",40,4],[4,1,\"David\",30,2],[5,1,\"Emma\",45,5],[6,2,\"Frank\",80,4],[7,2,\"Grace\",70,4],[8,2,\"Henry\",90,5],[9,2,\"Ivy\",60,4],[10,2,\"Jack\",75,4],[11,3,\"Kate\",100,2],[12,3,\"Liam\",120,1],[13,3,\"Mia\",80,2],[14,3,\"Noah\",90,1],[15,3,\"Olivia\",110,4],[16,3,\"Paul\",95,5],[17,4,\"Quinn\",150,3],[18,4,\"Ruby\",140,3],[19,5,\"Sam\",80,1],[20,5,\"Tara\",70,2]]}}", "__typename": "QuestionNode" } } }