본문 바로가기
데이터 분석/SQL 연습문제

Weather Observation Station 5 (HackerRank)

2022. 10. 12.
 

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.

풀이

제출한 답

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로만 간단하게 해결 가능

2022.10.12

'데이터 분석 > SQL 연습문제' 카테고리의 다른 글

SQL Project Planning (HackerRank)  (0) 2022.11.04
Binary Tree Nodes (HackerRank)  (0) 2022.11.03
Occupations (HackerRank)  (0) 2022.10.11
New Companies (HackerRank)  (0) 2022.10.05
Contest Leaderboard (HackerRank)  (0) 2022.10.04

댓글