본문 바로가기

Language/SQL

[HackerRank][SQL] Weather Observation Station 5 - 컴도리돌이

728x90
 

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


Problem

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.

-> station에서 가장 짧고 긴 도시 이름과 각각의 길이를 출력하시오. 가장 작은 도시 또는 가장 큰 도시가 두 개 이상 있는 경우, 알파벳 순으로 정렬하시오.


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