๐ป ๋ฌธ์ ์ฃผ์ : Weather Observation Station 3 | HackerRank
Weather Observation Station 3 | HackerRank
Query a list of unique CITY names with even ID numbers.
www.hackerrank.com
๐ป ๋ฌธ์
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. The STATION table is described as follows:
Column name | Type |
ID | Number |
CITY | Varchar2(21) |
STATE | Varchar2(2) |
LAT_N | Number |
LONG_W | Number |
๐ป ์ฝ๋ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
* ๋ณธ ๋ฌธ์ ๋ MYSQL๋ก ์์ฑํ์ต๋๋ค.
select distinct(city)
from station
where (id % 2) = 0
๐ป ๋ฌธ์ ํ์ด
๋ฌธ์ ์์ ์๊ตฌํ๋ ์กฐ๊ฑด์ ์๋์ ๊ฐ๋ค.
1๏ธโฃ a list of CITY that have an even ID number : ID๊ฐ ์ง์์ธ CITY
2๏ธโฃ exclude duplicates from the answer : ์ถ๋ ฅ ์ ์ค๋ณต ์ ๊ฑฐ
1๏ธโฃ a list of CITY that have an even ID number
ID๊ฐ ์ง์์ธ์ง ํ์์ธ์ง ๊ตฌํ๋ ๊ฒ์ ๋๋์ - ๋๋จธ์ง๋ฅผ ํ์ฉํ๋ฉด ๋๋ค. ์ฐ๋ฆฌ๊ฐ ํํ 2๋ก ๋๋์์ ๋ ๋๋จธ์ง๊ฐ 0์ด๋ฉด ์ง์, 1์ด๋ฉด ํ์๋ผ๊ณ ์๊ฐํ๋ฏ SQL ๋ํ ๋ง์ฐฌ๊ฐ์ง๋ค. ์ด๋ ๋๋จธ์ง ์ฐ์ฐ์ /๊ฐ ์๋ %๋ฅผ ํ์ฉํ๋ค.
select CITY
from STATION
where (ID % 2) = 0
2๏ธโฃ exclude duplicates from the answer
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ์ถ๋ ฅ ์ ์ค๋ณต์ ์ ๊ฑฐํด์ผ ํ๋ค. ๋ฌผ๋ก group by์ having์ ํตํด ์ค๋ณต์ ์ ๊ฑฐํ ์๋ ์๊ฒ ์ง๋ง ๊ทธ๋ณด๋ค ๋ ๊ฐ๋จํ๊ฒ select์์ ์ค๋ณต์ ์ ๊ฑฐํ๋ ํจ์์ธ distinct๋ฅผ ํ์ฉํ๋ฉด ๋๋ค.
select distinct(city)
from station
where (id % 2) = 0
'SQL > HackerRank' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] Weather Observation Station 18 (0) | 2024.05.03 |
---|---|
[SQL] The PADS (0) | 2024.04.30 |
[SQL] Weather Observation Station 5 (0) | 2024.04.24 |
[SQL] Revising the Select Query I (0) | 2024.03.11 |