LeetCode 620: Not Boring Movies

A SQL guide for filtering movies with odd IDs and non-boring descriptions, then sorting by rating.

Problem Restatement

We are given a table Cinema.

Each row represents a movie.

Column Type Meaning
id int Movie id
movie varchar Movie name
description varchar Movie description
rating float Movie rating

We need to:

  1. Select movies with odd id values
  2. Exclude movies whose description is "boring"
  3. Sort the result by rating in descending order

The official problem asks for filtering movies with odd IDs and non-boring descriptions, ordered by rating.

Input and Output

Input table:

Cinema

Output columns:

id, movie, description, rating

Example

Input:

id movie description rating
1 War great 3D 8.9
2 Science boring 8.5
3 Irish great 3D 6.2
4 Ice boring 8.6
5 House great 3D 9.1

Step 1: Keep odd IDs:

id
1
3
5

Step 2: Remove boring descriptions:

id movie description rating
1 War great 3D 8.9
3 Irish great 3D 6.2
5 House great 3D 9.1

Step 3: Sort by rating descending:

id movie rating
5 House 9.1
1 War 8.9
3 Irish 6.2

First Thought: Simple Filtering

We only need basic SQL filtering.

Two conditions:

Condition Meaning
id % 2 = 1 Odd IDs
description <> 'boring' Remove boring movies

Then apply sorting.

Key Insight

This is a straightforward WHERE + ORDER BY problem.

No joins, grouping, or aggregation are needed.

We just combine filtering conditions correctly.

Algorithm

Step 1: Filter rows where id is odd.

Step 2: Exclude rows where description is "boring".

Step 3: Sort remaining rows by rating DESC.

SQL Solution

SELECT
    id,
    movie,
    description,
    rating
FROM Cinema
WHERE id % 2 = 1
  AND description <> 'boring'
ORDER BY rating DESC;

Code Explanation

We select all required columns:

SELECT id, movie, description, rating
FROM Cinema

Then we apply the odd ID condition:

WHERE id % 2 = 1

This ensures only odd-numbered movies remain.

Next, we remove boring movies:

AND description <> 'boring'

Finally, we sort results:

ORDER BY rating DESC

So higher-rated movies appear first.

Correctness

The condition id % 2 = 1 correctly identifies all odd IDs because any integer id can be expressed as either 2k (even) or 2k + 1 (odd). Only values of the form 2k + 1 satisfy the modulo condition.

The condition description <> 'boring' removes all rows whose description exactly matches "boring", ensuring only non-boring movies remain.

Since both conditions are applied in the WHERE clause, only rows satisfying both constraints are included in the result set.

Finally, ordering by rating DESC ensures the remaining movies are sorted from highest to lowest rating.

Therefore, the query returns exactly the set of non-boring movies with odd IDs, sorted correctly by rating.

Complexity

Let n be the number of rows in Cinema.

Metric Value Why
Time O(n) One scan with filtering
Space O(1) No intermediate structures required

The database may internally use indexes on id or rating, but logically the operation is linear.

Alternative: Using BITWISE CHECK

Some SQL dialects support bitwise operations:

WHERE id & 1 = 1

This is equivalent to checking odd numbers but is less portable than modulo.

Testing

Sample data:

CREATE TABLE Cinema (
    id INT,
    movie VARCHAR(50),
    description VARCHAR(50),
    rating FLOAT
);

INSERT INTO Cinema VALUES
(1, 'War', 'great 3D', 8.9),
(2, 'Science', 'boring', 8.5),
(3, 'Irish', 'great 3D', 6.2),
(4, 'Ice', 'boring', 8.6),
(5, 'House', 'great 3D', 9.1);

Expected output:

id movie description rating
5 House great 3D 9.1
1 War great 3D 8.9
3 Irish great 3D 6.2

Additional case:

TRUNCATE TABLE Cinema;

INSERT INTO Cinema VALUES
(2, 'X', 'boring', 5.0),
(4, 'Y', 'boring', 7.0);

Expected output:

(empty result set)

All rows are either even or boring, so no output is returned.