본문 바로가기
MySQL/문제풀이

[LeetCode] 620. Not Boring Movies

by MINNI_ 2021. 3. 22.

1. 문제

 

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:

+---------+-----------+--------------+-----------+
|   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:

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

2. 답

SELECT *
FROM cinema
WHERE id % 2 != 0 AND description != "boring"
ORDER BY rating DESC

OR

SELECT *
FROM cinema
WHERE MOD(id, 2) = 1 AND description != "boring"
ORDER BY rating DESC

3. KEY POINT

 

  • ORDER BY 컬럼명 : 해당 컬럼 기준으로 정렬 ( 기본 : 오름차순 )
  • DESC : 내림차순
  • MOD(컬럼명/값, n) : 해당 값에 n을 나누었을 때의 나머지를 반환 = 컬럼명/값 % n
  • 홀수 = ( n % 2 = 1 ) = ( MOD(id, 2) = 1 )

 

 

leetcode.com/problems/not-boring-movies/

 

Not Boring Movies - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

댓글