[해커랭크/SQL]Weather Observation Station 5

canugo·2023년 3월 12일
0

해커랭크

목록 보기
10/12

문제

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.

Explanation

When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths and . 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.

문제풀이 & 해석

STATION에서 가장 짧은 도시 이름과 가장 긴 도시 이름과 각각의 길이(즉, 이름의 문자 수)를 쿼리합니다. 둘 이상의 가장 작은 도시 또는 가장 큰 도시가 있는 경우 알파벳 순으로 정렬할 때 가장 먼저 오는 도시를 선택합니다.

여기서 LAT_N은 북위, LONG_W는 서경이다.
샘플 입력
예를 들어 CITY에는 DEF, ABC, PQRS 및 WXY의 네 가지 항목이 있습니다.

CITY 이름이 알파벳 순으로 정렬되면 길이가 와 함께 ABC, DEF, PQRS 및 WXY로 나열됩니다. 가장 긴 이름은 PQRS이지만 가장 짧은 이름의 도시에 대한 옵션이 있습니다. ABC가 알파벳 순으로 먼저 나오므로 ABC를 선택합니다.

(SELECT CITY, LENGTH(CITY)
   FROM STATION
  WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION)
  ORDER BY CITY
  LIMIT 1)
UNION ALL
(SELECT CITY, LENGTH(CITY)
   FROM STATION
  WHERE LENGTH(CITY) = (SELECT MAX(LENGTH(CITY)) FROM STATION)
  ORDER BY CITY
  LIMIT 1)

0개의 댓글