CodingSpace

[HackerRank/SQL] Basic Select - Weather Observation Station 5 (feat. LENGTH) 본문

카테고리 없음

[HackerRank/SQL] Basic Select - Weather Observation Station 5 (feat. LENGTH)

개발자_조이킴 2022. 4. 24. 23:22

Problem. Basic Select - Weather Observation Station 5


Link.

https://www.hackerrank.com/challenges/weather-observation-station-5/problem?isFullScreen=true&h_r=next-challenge&h_v=zen 

 

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


Description.

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths. If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

 

STATION 테이블에서 도시 문자길이가장 짧은 도시와 가장 긴 도시를 각각 조회하는 SQL문을 작성하시오. 이때 각 도시의 문자길이도 반환하고 문자길이가 같은 도시가 존재하는 경우, 알파벳 순으로 반환하도록 하시오.

예) CITY = 'ABC', 'DEF', 'PQRS', 'WXY'

반환 값:

ABC 3

PQRS 4

STATION 테이블


Key Point. 

LENGTH 함수는 문자열 길이를 반환하는 함수이다.


My Answer. 

SELECT CITY, LENGTH(CITY) 
FROM STATION
ORDER BY LENGTH(CITY), CITY ASC 
LIMIT 1;
SELECT CITY, LENGTH(CITY) 
FROM STATION
ORDER BY LENGTH(CITY) DESC 
LIMIT 1;

References. 

LENGTH 함수: https://extbrain.tistory.com/65

 

[MySQL] 문자열 길이 가져오기 (LENGTH, CHAR_LENGTH 함수)

▶MySQL 문자열 길이 가져오기 (LENGTH, CHAR_LENGTH 함수) ▶설명 MySQL에서 문자열의 길이를 가져올 때, 사용하는 것이  LENGTH함수입니다. 그렇지만, LENGTH 함수는 문자의 Byte길이를 가져오기 때문에 한

extbrain.tistory.com

 

Comments