KEEP GOING

[MySQL] HackerRank : Weather Observation Station 11 Solution 본문

code review/sql

[MySQL] HackerRank : Weather Observation Station 11 Solution

jmHan 2021. 12. 21. 12:06
반응형

https://www.hackerrank.com/challenges/weather-observation-station-11/problem

 

Weather Observation Station 11 | HackerRank

Query a list of CITY names not starting or ending with vowels.

www.hackerrank.com

 

Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

 

 

(1) LEFT, RIGT 사용 

SELECT DISTINCT CITY FROM STATION 
       WHERE (LEFT(LOWER(CITY), 1) NOT IN ('a','e','i','o','u') OR
             RIGHT(LOWER(CITY), 1) NOT IN ('a','e','i','o','u'))

모음의 종류로는 a, e, i, o, u가 있다. (해당 문제에서는 y 제외)

 

 

(2) 정규표현식 사용 

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[^aeiou].*' OR CITY REGEXP '.*[^aeiou]$'

 

[]안에서의 ^ : 부정

[aeiou] : aeiou 중 하나의 문자 

[]밖에서의 ^ : 시작의 의미

^[aeiou] : 맨 앞글자가 모음  ex) 'a', 'e', 'i', 'o', 'u'   'alien'이라는 문자열 중 'a'만 선택됨

.* : .의 반복 

^[aeiou].* : 모음으로 시작하는 문자열 ex) 'alien', 'elbow', 'illusion' 

^[^aeiou].* : 모음으로 시작하지 않는 문자열 ex) 'dark', 'popping'

$ : 끝을 의미 

[aeiou]$ : 맨 끝글자가 자음

.*[aeiou]$ : 모음으로 끝나는 문자열 

.*[^aeiou]$ : 모음으로 끝나지 않는 문자열 

 

 

반응형
Comments