본문 바로가기

SQL/예제로 익히는 SQL 함수

MOD | SQL에서 나머지와 몫 (짝수, 홀수)

https://leetcode.com/problems/not-boring-movies/description/

더보기

Table: Cinema

+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| id             | int      |
| movie          | varchar  |
| description    | varchar  |
| rating         | float    |
+----------------+----------+
id is the primary key (column with unique values) for this table.
Each row contains information about the name of a movie, its genre, and its rating.
rating is a 2 decimal places float in the range [0, 10]


Write a solution to report the movies with an odd-numbered ID and a description 
that is not "boring".
Return the result table ordered by rating in descending order.
The result format is in the following example.

 

Write a solution to report the movies with an odd-numbered ID and a description that is not "boring".

Return the result table ordered by rating in descending order.

The result format is in the following example.

▶️ 내 코드(정답)

select *
from cinema
where id%2 = 1
and description not like '%boring%'
order by rating desc

 

👁️👁️ 참고할 코드 (MOD 활용)

SELECT * FROM Cinema
WHERE MOD(id, 2) <> 0 AND description <> 'boring'
ORDER BY rating DESC

 

MOD(나눌 값 혹은 컬럼 / 나눌 숫자) -> 나머지 출력

 

위 코드는 id 컬럼의 값들을 2로 나눴을 때, 0이 아닌 나머지,

즉 나머지가 1인 홀수 id들을 걸러주는 조건을 준 것이다. 

 

++참고

FLOOR (나눠질 값 혹은 컬럼 / 나눌 숫자) -> 몫 출력