Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/SQL Query test

[MySQL] HackerRank - Weather Observation Station 5

๊ฐ์ž ๐Ÿฅ” 2021. 7. 22. 21:12
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ

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

 

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 longest 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:

where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input
For example, CITY has four entries: DEF, ABC, PQRS and WXY.
Sample Output

Explanation:

When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths 3, 3, 4 and 3. The longest name is PQRS, but there are  options for shortest named city. Choose ABC, because it comes first alphabetically.

Note:
You can write two separate queries to get the desired output. It need not be a single query.

ํ•ด์„) CITY์—์„œ ๊ฐ€์žฅ ์งง์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋„์‹œ์™€, ๊ฐ€์žฅ ๊ธด ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋„์‹œ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ. 

 

์ •๋‹ต

์•„๋‹ˆ ์ฟผ๋ฆฌ ๋‘๊ฐœ๋กœ ์ž…๋ ฅํ•˜๋ผ๊ทธ๋ž˜์„œ ๋‘๊ฐœ๋กœ๋งŒ ์ž…๋ ฅํ–ˆ๋”๋‹ˆ ๊ณ„์† ์˜ค๋ฅ˜๋œจ๊ธธ๋ž˜ ์ธํ„ฐ๋„ท ์ฐพ์•„๋ณด๋‹ˆ๊นŒ UNION ์œผ๋กœ ๋ฌถ์€๊ฒƒ์„ ๋ณผ ์ˆ˜์žˆ์—ˆ๋‹ค. ์•„๋‹ˆ ์ฟผ๋ฆฌ ๋‘๊ฐœ๋กœ ํ•˜๋ผ๋ฉด์„œ ใ…   UNION์œผ๋กœ ๋ฌถ์œผ๋ฉด ์ด๊ฒŒ ํ•œ๊ฐœ์ง€ ๋‘๊ฐœ๋ƒ!!! ์–ด์จ‹๋“  ํ†ต๊ณผ...

(
    SELECT CITY, LENGTH(CITY)
    FROM STATION
    ORDER BY LENGTH(CITY) ASC, CITY ASC
    LIMIT 1
)

UNION

(
    SELECT CITY, LENGTH(CITY)
    FROM STATION
    ORDER BY LENGTH(CITY) DESC, CITY ASC
    LIMIT 1
)

๋ฐ˜์‘ํ˜•