๐ป ๋ฌธ์ ์ฃผ์ : 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, ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ๊ฒ์ด ํ์ํ์ง ์๋? ์ถ์๊ธฐ ๋๋ฌธ์ด๋ค. ์๋ฌด๋๋ ์ด ๋ถ๋ถ์ ์ข ๋ ๊ณ ๋ฏผํด๋ด์ผ ํ ๋ฏํ๋ค.
'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 |