데이터 분석/SQL 연습문제
Weather Observation Station 5 (HackerRank)
중급닌자 연습생
2022. 10. 12. 21:01
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.
풀이
제출한 답
select city, length(city)
from station
where length(city) = (select min(length(city))
from station)
order by city
limit 1;
select city, length(city)
from station
where length(city) = (select max(length(city))
from station)
-> where 절에 서브쿼리로 조건 부여 / 그냥 select 절에 max(length(city)) 하면 모든 city명과 max 값이 줄줄이 출력됨
다른 답
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;
-> 최대 값, 최소 값 구할 때 집계함수 사용하지 않고 정렬과 limit 1로만 간단하게 해결 가능