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 )
'MySQL > 문제풀이' 카테고리의 다른 글
[LeetCode] 175. Combine Two Tables (0) | 2021.03.22 |
---|---|
[LeetCode] 182. Duplicate Emails (0) | 2021.03.22 |
[LeetCode] 595. Big Countries (0) | 2021.03.22 |
[LeetCode] CASE 테이블 피봇 : 1179. Reformat Department Table (0) | 2021.03.15 |
[HackerRank] Self JOIN: Symmetric Pairs (0) | 2021.03.15 |
댓글