Not Boring Movies - Problem

๐ŸŽฌ Cinema Database Query Challenge

You're working as a data analyst for a movie streaming platform! The cinema database contains information about various movies including their ID, title, genre description, and rating.

Your task is to help users discover interesting movies by filtering out the boring ones. Specifically, you need to:

  • Find movies with odd-numbered IDs (1, 3, 5, 7, etc.)
  • Exclude movies where the description is exactly "boring"
  • Return results ordered by rating in descending order (highest rated first)

Goal: Write a SQL query that returns the filtered movie list to help users find the most interesting, highly-rated films!

Input & Output

example_1.sql โ€” Basic Filtering
$ Input: Cinema table: +----+----------+-------------+--------+ | id | movie | description | rating | +----+----------+-------------+--------+ | 1 | War | great 3D | 8.9 | | 2 | Science | fiction | 8.5 | | 3 | irish | boring | 6.2 | | 4 | Ice song | Fantacy | 8.6 | | 5 | House card| Interesting| 9.1 | +----+----------+-------------+--------+
โ€บ Output: +----+------------+-------------+--------+ | id | movie | description | rating | +----+------------+-------------+--------+ | 5 | House card | Interesting | 9.1 | | 1 | War | great 3D | 8.9 | +----+------------+-------------+--------+
๐Ÿ’ก Note: Movies with odd IDs (1, 3, 5) are candidates. Movie ID 3 is excluded because description is 'boring'. Remaining movies (1, 5) are sorted by rating DESC: 9.1, then 8.9.
example_2.sql โ€” All Movies Boring
$ Input: Cinema table: +----+----------+-------------+--------+ | id | movie | description | rating | +----+----------+-------------+--------+ | 1 | Action | boring | 7.0 | | 3 | Comedy | boring | 8.0 | | 5 | Drama | boring | 9.0 | +----+----------+-------------+--------+
โ€บ Output: Empty result set +----+-------+-------------+--------+ | id | movie | description | rating | +----+-------+-------------+--------+ +----+-------+-------------+--------+
๐Ÿ’ก Note: All movies with odd IDs have 'boring' descriptions, so they are all filtered out, resulting in an empty result set.
example_3.sql โ€” Single Movie Result
$ Input: Cinema table: +----+----------+-------------+--------+ | id | movie | description | rating | +----+----------+-------------+--------+ | 1 | Avengers | superhero | 9.5 | | 2 | Titanic | romance | 8.8 | +----+----------+-------------+--------+
โ€บ Output: +----+----------+-------------+--------+ | id | movie | description | rating | +----+----------+-------------+--------+ | 1 | Avengers | superhero | 9.5 | +----+----------+-------------+--------+
๐Ÿ’ก Note: Only movie with ID 1 has an odd ID. It's not boring, so it's included in the result. ID 2 is even, so excluded.

Visualization

Tap to expand
Cinema Database Query ExecutionOriginal Cinema TableID | Movie | Description | Rating1 | War | great 3D | 8.92 | Science | fiction | 8.53 | Irish | boring | 6.24 | Ice Song | Fantasy | 8.65 | House Card | Interesting | 9.1Step 1: Filter Conditionsโœ“ ID % 2 = 1 (odd numbers)โœ“ description != 'boring'Rows 1 and 5 pass filtersRow 3: odd but boring โœ—Step 2: Sort by Rating DESC5 | House Card | 9.1 โญ1 | War | 8.9 โญHighest rating firstSQL Query BreakdownSELECT Clauseid, movie, description, ratingChoose columns to returnWHERE Clauseid % 2 = 1 AND description != 'boring'Filter conditionsORDER BY Clauserating DESCSort highest to lowestComplete QuerySELECT * FROM Cinema WHERE id % 2 = 1 AND description != 'boring' ORDER BY rating DESC;
Understanding the Visualization
1
Scan All Movies
Database engine examines each movie record in the Cinema table
2
Check ID Parity
For each movie, calculate ID % 2 to determine if it's odd (remainder = 1)
3
Check Description
Verify that the description field is not exactly equal to 'boring'
4
Collect Matches
Gather all movies that pass both filter conditions
5
Sort by Rating
Arrange the filtered results in descending order by rating (highest first)
Key Takeaway
๐ŸŽฏ Key Insight: SQL databases excel at filtering and sorting operations. Use WHERE clauses for filtering conditions and ORDER BY for sorting - let the database engine optimize the execution plan for you!

Time & Space Complexity

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

Linear scan through all records to apply filters, plus O(m log m) for sorting filtered results where m is the number of matching records

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

Database query uses constant additional space for filtering operations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค id โ‰ค 104
  • 1 โ‰ค movie.length โ‰ค 100
  • 1 โ‰ค description.length โ‰ค 100
  • 0.0 โ‰ค rating โ‰ค 10.0 (with exactly 2 decimal places)
  • All movie names and descriptions contain only printable ASCII characters
Asked in
Meta 25 Amazon 18 Google 15 Microsoft 12
58.0K Views
Medium Frequency
~8 min Avg. Time
1.5K 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