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

[SQL] Weather Observation Station 5

by ์ด๋ฎด 2024. 4. 24.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : Weather Observation Station 5 | HackerRank

 

Weather Observation Station 5 | HackerRank

Write a query to print the shortest and longest length city name along with the length of the city names.

www.hackerrank.com

 

๐Ÿ’ป ๋ฌธ์ œ

Query the two cities in STATION with the shortest and lognest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

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 city, length(city)
from STATION
order by length(city), city
limit 1;

select city, length(city)
from STATION
order by length(city) desc, city
limit 1;

 

๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

1๏ธโƒฃ Query the two cities with the shortest and longest CITY names, and their respective lengths
2๏ธโƒฃ Choose the one that comes first when ordered alphabetically

 

1๏ธโƒฃ Query the two cities with the shortest and longest CITY names, and their respective lengths

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” (1) ๊ฐ€์žฅ ์งง์€ ๋„์‹œ๋ช… (2) ๊ฐ€์žฅ ๊ธด ๋„์‹œ๋ช…์„ ๊ฐ€์ง„ ๊ฐ ๋„์‹œ์˜ ๋ช…์นญ๊ณผ ๋ช…์นญ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋‹คํ–‰ํžˆ ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” ์ฟผ๋ฆฌ ํ•˜๋‚˜์— (1), (2)์— ๋Œ€ํ•œ ์ถœ๋ ฅ์„ ๋‹ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค๊ณ  ํ•˜๋‹ˆ select๋ถ€ํ„ฐ ์›ํ•˜๋Š” ๋Œ€๋กœ ์จ๋ณด์ž.

-- (1) ๊ฐ€์žฅ ์งง์€ ๋„์‹œ๋ช…
select city, length(city)
from STATION

-- (2) ๊ฐ€์žฅ ๊ธด ๋„์‹œ๋ช…
select city, length(city)
from STATION

 

๊ทธ ํ›„์—” ์ด์ œ 2๏ธโƒฃ ๋กœ ๋„˜์–ด๊ฐ€๋ฉด ๋œ๋‹ค.

 

2๏ธโƒฃ Choose the one that comes first when ordered alphabetically

๋‹คํ–‰ํžˆ ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ Join์ด๋‚˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ ๋„ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์กฐ๊ฑด์ด๋‹ค. ๋ฐ”๋กœ '์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๊ฒฐ๊ณผ 1๊ฐœ๋งŒ ๋‚˜์˜ค๋„๋ก ํ•  ๊ฒƒ'. ์›๋ž˜์˜€๋‹ค๋ฉด min(length(city)) ์ฆ‰, ๊ฐ€์žฅ ์งง์€/๊ธด ๋„์‹œ๋ช…์„ ๊ฐ€์ง„ ๋„์‹œ๋“ค์„ ๋จผ์ € ์ถ”์ถœํ•œ ๋‹ค์Œ, ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๊ทธ ์ค‘ ์ œ์ผ ์•ž์— ์žˆ๋Š” ๊ฐ’์„ ๊ณ ๋ฅด๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

์œ„ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ ์˜ฎ๊ฒจ๋ณด์ž. ๋จผ์ € ๋„์‹œ๋ช… ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ '๊ธด' ๋„์‹œ๋ช…์˜ ๊ฒฝ์šฐ, ๋„์‹œ๋ช… ๊ธธ์ด๋ฅผ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ๋’ค์— ์œ„์น˜ํ•˜์˜€์„ ๊ฒƒ๋ถ€ํ„ฐ ์•ž์œผ๋กœ ์„ธ์›Œ์•ผ ํ•˜๋‹ˆ ๋‚ด๋ฆผ์ฐจ์ˆœ, ์ฆ‰ desc๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค. 

-- (1) ๊ฐ€์žฅ ์งง์€ ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city)

-- (2) ๊ฐ€์žฅ ๊ธด ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city) desc -- ๋‚ด๋ฆผ์ฐจ์ˆœ

 

์˜ˆ๋ฅผ ๋“ค์–ด '์„œ์šธ / ์ˆ˜์› / ์ธ์ฒœ' ์ฒ˜๋Ÿผ ๋งŒ์•ฝ ๋„์‹œ๋ช… ๊ธธ์ด๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ, ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ  ์„ฐ์„ ๋•Œ ์ œ์ผ ์•ž์— ์žˆ๋Š” ๋„์‹œ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์ด ๋˜ํ•œ ์ •๋ ฌ์„ ํ•ด์•ผ ํ•˜๊ธฐ์— order by์— ๊ฐ™์ด ๋„ฃ์–ด์ค€๋‹ค.

-- (1) ๊ฐ€์žฅ ์งง์€ ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city), city

-- (2) ๊ฐ€์žฅ ๊ธด ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city) desc, city

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด์ œ ์šฐ๋ฆฌ๋Š” Choose one, ๋‹ค์‹œ ๋งํ•ด ๊ธฐ์ค€์— ๋งž๋Š” ๋„์‹œ ๋‹จ ํ•œ ๊ตฐ๋ฐ๋งŒ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ limit๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋๋‚œ๋‹ค.

-- (1) ๊ฐ€์žฅ ์งง์€ ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city), city
limit 1

-- (2) ๊ฐ€์žฅ ๊ธด ๋„์‹œ๋ช…
select city, length(city)
from STATION
order by length(city) desc, city
limit 1

 


ํ•œ๋™์•ˆ ๋ฐ”์‚ ์‚ด๋‹ค ์˜ค๋žœ๋งŒ์— HackerRank์—์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์„œ์ธ์ง€ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ƒ๊ฐ์ง€๋„ ๋ชปํ•˜๊ฒŒ ํ‘ธ๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค. ์‰ฝ๊ฒŒ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋Š”๋ฐ ์„œ๋ธŒ์ฟผ๋ฆฌ๋งŒ์„ ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์‚ฌ์‹ค ์ฝ”๋“œ ์ž์ฒด๋Š” ํ˜„์—…์— ์ผํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋ด๋„ ์ด์ƒ ์—†๋‹ค๊ณ ๋Š” ํ•˜์ง€๋งŒ ์ž๊พธ Syntax Error๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ๋” ์‰ฌ์šด ๋ฐฉ์•ˆ์„ ๊ณ ์•ˆํ•œ ๊ฒƒ์ด๋‹ค.

 

๋‹ค๋งŒ ์ด ๋ฐฉ์•ˆ์—๋„ ํ•œ ๊ฐ€์ง€ ์˜๋ฌธ์ด ์žˆ๋‹ค.

 

์ด๋ฒˆ ๋ฌธ์ œ ์ค‘ (2) ๊ธด ๋„์‹œ๋ช…์„ ๊ฐ€์ง„ ๋„์‹œ์™€ ๋„์‹œ๋ช… ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ, ๋‹ค๋ฅธ ๋ถ„๋“ค์ด ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‹ˆ order by length(city) desc, city desc๋ผ๊ณ  ๋‚˜์™€์žˆ์—ˆ๋‹ค. ๊ธด ๋„์‹œ๋ช…์„ ๊ตฌํ•ด์•ผ ํ•˜๋‹ˆ length(city)๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์€ ๋งž์œผ๋‚˜, ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ ์ œ์ผ ์•ž์ˆœ์œ„์ธ ๋„์‹œ ํ•˜๋‚˜๋งŒ์„ ์ถ”์ถœํ•˜๋ ค๋ฉด asc, ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‚˜? ์‹ถ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์•„๋ฌด๋ž˜๋„ ์ด ๋ถ€๋ถ„์€ ์ข€ ๋” ๊ณ ๋ฏผํ•ด๋ด์•ผ ํ•  ๋“ฏํ•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

'SQL > HackerRank' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SQL] Weather Observation Station 18  (0) 2024.05.03
[SQL] The PADS  (0) 2024.04.30
[SQL] Weather Observation Station 3  (0) 2024.03.12
[SQL] Revising the Select Query I  (0) 2024.03.11