본문 바로가기
Algorithm & SQL/SQL

Weather Observation Station 5

by YoonJong 2022. 6. 7.
728x90

 

 STATION 테이블에서 city 기준으로, city 이름이 가장 짧은 것과 , 가장 긴 것 을 출력하는 문제이다.

출력할 값의 길이가 같은 경우 이름이 빠른것을 출력해야 한다.

 

오름차순, 내림차순을 이용해야하며,  

-> 오름차순 asc ( 생략가능)  / 내림차순 desc

짧은거 1개, 긴 것 1개를 출력해야하기 때문에 limit 을 사용해서 출력해야한다. 

-> 1개만 출력할때는 limit 1 을 사용한다.

출력값은 city 이름과 city이름의 길이를 출력한다.

 

쿼리문을 2개 작성해야 하기 때문에 두개를 나눠서 작성하고 이어주려면 끝에 ; 를 사용해야한다.

 

 

 

다시풀어보기  : select city, length(city) from station order by length(city) asc , city asc limit  1;

                          select city, length(city) from station order by length(city) desc, city asc limit 1;

 

 

정답 :select city , length(city) from station order by length(city) asc , city asc limit 1;
select city , length(city) from station order by length(city) desc , city asc limit 1;

728x90

'Algorithm & SQL > SQL' 카테고리의 다른 글

Weather Observation Station 7  (0) 2022.06.07
Weather Observation Station 6 (regexp)  (0) 2022.06.07
Weather Observation Station 4  (0) 2022.06.07
Weather Observation Station 3  (0) 2022.06.07
Japanese Cities' Names  (0) 2022.06.07

댓글