본문 바로가기

SQL/HackerRank

[SQL] HackerRank : Weather Observation Station 3

문제

 

Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.

 

실행코드

 

 

# cities that have an even ID number

-> MOD 함수 이용MOD(id,2) : id 값을 2로 나눈 나머지 출력 -> 2로 나눈 나머지가 0이면 짝수

 

# exclude duplicates from the answer -> DISTINCT를 이용해 중복 값 제거

 

 

 

+다른 풀이 방법(feat. SQL스터디)

 

#1

 

일의 자리 숫자가 0,2,4,6,8이면 짝수

-> RIGHT 함수로 ID의 일의 자리 숫자 추출에서 짝수 번째 ID 찾기

 

SELECT DISTINCT city
FROM station
WHERE RIGHT(ID, 1) IN (2, 4, 6, 8, 0)

 

 

#2

(a%b) : a/b 나머지 출력

 

SELECT DISTINCT city
FROM station
WHERE (ID % 2) = 0

 

'SQL > HackerRank' 카테고리의 다른 글

[SQL] HackerRank: Symmetric Pairs  (0) 2023.06.01
[SQL] HackerRank: INNER JOIN 문제  (0) 2023.05.31
[SQL] HakerRank: The Report  (0) 2023.05.31
[SQL] HackeRank : Weather Observation Station 11  (0) 2023.05.19
[SQL] HackerRank : The Blunder  (0) 2023.05.18