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

코드설명

UNION + LENGTH + ROWNUM + ORDER BY 을 활용합니다.

 

문제에서 원하는대로 각각 하나씩 구해서 UNION ALL로 중복제거 없이 합칩니다.

한개의 쿼리로만은 처리하지 못하는 문제입니다.

ORACLE 코드

SELECT CITY, LENGTH(CITY)
FROM (
    SELECT CITY, LENGTH(CITY)
    FROM STATION 
    ORDER BY LENGTH(CITY) ASC, CITY ASC 
)
WHERE ROWNUM = 1
UNION ALL
SELECT CITY, LENGTH(CITY)
FROM (
    SELECT CITY, LENGTH(CITY)
    FROM STATION 
    ORDER BY LENGTH(CITY) DESC, CITY ASC 
)
WHERE ROWNUM = 1;

+ Recent posts