[해커랭크] Weather Observation Station 1 / 3 / 4

june·2023년 3월 19일
0

SQL

목록 보기
2/31

MySQL

Weather Observation Station 1

https://www.hackerrank.com/challenges/weather-observation-station-1

  • Query a list of CITY and STATE from the STATION table.
SELECT city, state
FROM station

Weather Observation Station 3

https://www.hackerrank.com/challenges/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.
SELECT DISTINCT city
FROM station
WHERE id % 2 = 0

Lesson & Learned

  1. id가 짝수인 조건에서 id % 2 == 0 을 썼는데 python문법과 혼동 주의하자.
  2. 나머지 구하기 함수 MOD(x, y) : x를 y로 나눈 나머지를 반환
    2-1. 조건 부여 시, 별도 연산을 걸지 않는 것이 좋다.(링크)
WHERE MOD(id, 2) = 0

Weather Observation Station 4

https://www.hackerrank.com/challenges/weather-observation-station-4

  • Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
SELECT COUNT(city) - COUNT(DISTINCT city)
FROM station
profile
나의 계절은

0개의 댓글