Books with NULL Ratings - Problem

You're working as a data analyst for a digital library platform, and you need to identify which books in the catalog are missing ratings. This information is crucial for the recommendation system to decide which books need more user engagement to generate ratings.

Given a books table with columns for book_id, title, author, published_year, and rating, your task is to find all books that haven't been rated yet (where rating is NULL).

Your Goal: Write a SQL query to retrieve all books with NULL ratings, ordered by book_id in ascending order. Return all columns except the rating column since we already know it's NULL.

Note: This is a fundamental SQL problem that teaches you how to handle NULL values using the IS NULL condition.

Input & Output

example_1.sql โ€” Basic Query
$ Input: books table with 6 records, 3 having NULL ratings
โ€บ Output: 3 books with NULL ratings ordered by book_id
๐Ÿ’ก Note: The query filters out books with actual ratings and returns only those where rating IS NULL, sorted by book_id in ascending order.
example_2.sql โ€” Edge Case: All Books Rated
$ Input: books table where all books have ratings (no NULLs)
โ€บ Output: Empty result set
๐Ÿ’ก Note: When no books have NULL ratings, the query returns an empty result set, which is the correct behavior.
example_3.sql โ€” Edge Case: All Books Unrated
$ Input: books table where all books have NULL ratings
โ€บ Output: All books returned in book_id ascending order
๐Ÿ’ก Note: When all books have NULL ratings, all records are included in the result, demonstrating the filter works correctly in edge cases.

Visualization

Tap to expand
Digital Library: Finding Unrated BooksAll Books in Catalog:Book 1Gatsby4.5Book 2MockingbirdNo RatingBook 3Pride4.8Book 4CatcherNo RatingBook 5Animal Farm4.2Book 6Lord FliesNo RatingIS NULL FilterFiltered Results (Unrated Books Only):Book 2ID: 2MockingbirdHarper Lee1960Book 4ID: 4CatcherSalinger1951Book 6ID: 6Lord FliesGolding1954Books are automatically sorted by ID (2, 4, 6)
Understanding the Visualization
1
Library Scan
We systematically go through each book in our digital catalog
2
Rating Check
For each book, we check if there's a rating sticker (not NULL) or an empty slot (IS NULL)
3
Collection
We collect all books that have empty rating slots into our 'needs attention' pile
4
Organization
Finally, we organize our collection by book ID number for easy reference
Key Takeaway
๐ŸŽฏ Key Insight: SQL's IS NULL condition is the proper way to identify missing values. Unlike regular comparisons, NULL values require special handling because NULL represents 'unknown' rather than a specific value.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single scan through all records in the table

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for k records that match the NULL condition

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค book_id โ‰ค 106
  • 1 โ‰ค title.length, author.length โ‰ค 100
  • 1800 โ‰ค published_year โ‰ค 2024
  • rating can be NULL or a decimal between 0.0 and 5.0
Asked in
Amazon 85 Google 72 Microsoft 68 Meta 45
89.5K Views
High Frequency
~8 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen