HackerRank

[HackerRank Easy] Weather Observation Station 5[MySQL]

치킨먹고싶어요 2022. 5. 27. 00:44

https://www.hackerrank.com/challenges/weather-observation-station-5/problem?isFullScreen=true 

 

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

 

Answer:

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;
cs

Question:

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

ABC 3
PQRS 4

풀이:

길이 기준으로 정렬 후, 알파벳순으로 도시를 나열하여 하나 출력합니다

그리고 마찬가지의 방법으로 도시를 역순으로 나열하여 하나 출력합니다