Books with NULL Ratings - Problem

Given a books table containing information about books including their ratings, find all books that have not been rated yet (i.e., have a NULL rating).

The books table contains:

  • book_id: Unique identifier for each book
  • title: Book title
  • author: Book author
  • published_year: Year the book was published
  • rating: Book rating (can be NULL if not rated)

Return the result ordered by book_id in ascending order. Exclude the rating column from the output since all returned books will have NULL ratings.

Table Schema

books
Column Name Type Description
book_id PK int Unique identifier for each book
title varchar Title of the book
author varchar Author of the book
published_year int Year the book was published
rating decimal Book rating, can be NULL if not rated
Primary Key: book_id
Note: Each row represents one book. The rating column can contain NULL values for unrated books.

Input & Output

Example 1 — Mixed Rated and Unrated Books
Input Table:
book_id title author published_year rating
1 The Great Gatsby F. Scott Fitzgerald 1925 4.5
2 To Kill a Mockingbird Harper Lee 1960
3 Pride and Prejudice Jane Austen 1813 4.8
4 The Catcher in the Rye J.D. Salinger 1951
5 Animal Farm George Orwell 1945 4.2
6 Lord of the Flies William Golding 1954
Output:
book_id title author published_year
2 To Kill a Mockingbird Harper Lee 1960
4 The Catcher in the Rye J.D. Salinger 1951
6 Lord of the Flies William Golding 1954
💡 Note:

The query filters books where rating IS NULL. Books with IDs 2, 4, and 6 have NULL ratings, so they are included in the result. The other books have actual rating values (4.5, 4.8, 4.2) and are excluded. Results are ordered by book_id in ascending order.

Example 2 — All Books Have Ratings
Input Table:
book_id title author published_year rating
1 Book One Author A 2020 4
2 Book Two Author B 2021 3.5
Output:
book_id title author published_year
💡 Note:

When all books have ratings (no NULL values), the query returns an empty result set since no books match the rating IS NULL condition.

Example 3 — All Books Unrated
Input Table:
book_id title author published_year rating
1 New Book New Author 2023
3 Another Book Another Author 2023
Output:
book_id title author published_year
1 New Book New Author 2023
3 Another Book Another Author 2023
💡 Note:

When all books have NULL ratings, all books are returned in the result, ordered by book_id ascending.

Constraints

  • 1 ≤ book_id ≤ 10000
  • title and author are non-empty strings
  • published_year is a valid year
  • rating can be NULL or a decimal value between 0.0 and 5.0

Visualization

Tap to expand
Books with NULL Ratings - SQL Problembooks tablebook_idtitlerating1Gatsby4.52MockingbirdNULL3Pride4.84CatcherNULLWHERE ratingIS NULLResult: Unrated Booksbook_idtitleauthor2MockingbirdLee4CatcherSalingerOnly books with NULL ratings are returned
Understanding the Visualization
1
Input Table
Books table with some NULL ratings
2
NULL Filter
WHERE rating IS NULL condition
3
Output
Only unrated books returned
Key Takeaway
🎯 Key Insight: Use IS NULL (not = NULL) to find missing values in SQL
Asked in
Amazon 15 Microsoft 12 Google 8
23.4K Views
High Frequency
~8 min Avg. Time
892 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