๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/HackerRank

[SQL] Weather Observation Station 3

by ์ด๋ฎด 2024. 3. 12.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : 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

 

 

728x90
๋ฐ˜์‘ํ˜•

'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