sql notes: leetcode#620 not boring movies

Problem


X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.

Please write a SQL query to output movies with an odd numbered ID and a description that is not ‘boring’. Order the result by rating.

For example, table cinema:

1
2
3
4
5
6
7
8
9
+---------+-----------+--------------+-----------+
| 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 |
+---------+-----------+--------------+-----------+

For the example above, the output should be:

1
2
3
4
5
6
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
+---------+-----------+--------------+-----------+

Analysis


  • Output all columns from table cinema:
1
SELECT * FROM cinema;
  • Output movies with an odd numbered ID and a description that is not ‘boring’:
1
SELECT * FROM cinema WHERE id % 2 <> 0 AND description <> 'boring';
  • Order the result by rating:
1
SELECT * FROM cinema WHERE id % 2 <> 0 AND description <> 'boring' ORDER BY rating DESC;

Solution


1
SELECT * FROM cinema WHERE id % 2 <> 0 AND description <> 'boring' ORDER BY rating DESC;

620. Not Boring Movies
(中文版) SQL 笔记: Leetcode#620 Not Boring Movies