IT/코테문제

[Hacker Rank_MySQL]문제 풀이#Weather Observation Station 5

호랑구야 2022. 2. 21. 09:00

SQL > Basic Select > Weather Observation Station 5

 

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.

 

Note

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

빨간 글씨는 내가 문제를 풀 때 중요하다고 생각한 부분이다.

도시 이름의 길이가 가장 짧은 것과 가장 긴 것의 도시 이름과 그 길이를 가져오라고 하는데, 만약 길이가 동일한 도시가 여러개 있을 경우에는 알파벳순으로 가장 먼저 오는 것을 고르라고 한다. 또한 문제에서 쿼리를 2개 써야 원하는 것을 얻을 수 있다고 알려주었다. 

 

 

/*
USING LIMIT 1
*/
SELECT *
FROM (
    SELECT CITY, LENGTH(CITY)
    FROM STATION
    ORDER BY LENGTH(CITY) ASC, CITY ASC
    LIMIT 1
) X
UNION
SELECT *
FROM (
    SELECT CITY, LENGTH(CITY)
    FROM STATION
    ORDER BY LENGTH(CITY) DESC, CITY ASC
    LIMIT 1
) Y

 

반응형