[hackerrank] Prepare > SQL > Basic Select > Weather Observation Station 5
[문제]
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
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.
[MYSQL]
[ORCALE]
select city , length(city) len
from station
order by len asc,city asc;
=>여기까지 적고 막혀있었는데
막혀있던 이유가 문제를 잘못이해했고.. 그래서 생각이 멈춘 듯..답은
select *
from (
select city ,length(city) len
from station
order by len desc, city asc)
where rownum = 1;
select *
from (
select city, length(city) len
from station
order by len asc, city asc)
where rownum = 1;
허허..하나의 쿼리만으로 작성할 필요가 없으며
단어의 길이가 가장 긴 것 하나
단어의 길이가 가장 짧은 것 하나를 출력하면 된다.
만일 단어의 길이가 같은 것이 여러개라면
이름으로 정렬했을 때 가장 앞에 오는 것을 뽑으면 된다.
'HackerRank > SQL' 카테고리의 다른 글
[hackerrank] Weather Observation Station 7 (0) | 2022.08.08 |
---|---|
[hackerrank] Weather Observation Station 6 (0) | 2022.08.08 |
[hackerrank] Weather Observation Station 4 (0) | 2022.08.08 |
[hackerrank] Weather Observation Station 3 (0) | 2022.08.08 |
[hackerrank] Weather Observation Station 1 (0) | 2022.08.08 |