LeetCode solutions

3996. Find Books with Polarized Opinions

My accepted SQL solution to LeetCode problem 3996, Find Books with Polarized Opinions, running in 512ms.

  • Difficulty: Easy
  • SQL
  • Runtime 512ms
  • Memory 0.0B

Read the problem on LeetCode View on GitHub

SQL

Accepted on LeetCode — runtime 512ms, memory 0.0B, accepted 2026-01-02.

sql
SELECT 
    b.book_id,
        b.title,
            b.author,
                b.genre,
                    b.pages,
                        MAX(r.session_rating) - MIN(r.session_rating) AS rating_spread,
                            ROUND(SUM(CASE WHEN r.session_rating <= 2 OR r.session_rating >= 4 THEN 1 ELSE 0 END) * 1.0 / COUNT(*), 2) AS polarization_score
                            FROM books b
                            JOIN reading_sessions r ON b.book_id = r.book_id
                            GROUP BY b.book_id, b.title, b.author, b.genre, b.pages
                            HAVING 
                                COUNT(*) >= 5
                                    AND MAX(r.session_rating) >= 4
                                        AND MIN(r.session_rating) <= 2
                                            AND SUM(CASE WHEN r.session_rating <= 2 OR r.session_rating >= 4 THEN 1 ELSE 0 END) * 1.0 / COUNT(*) >= 0.6
                                            ORDER BY polarization_score DESC, title DESC;

Source