인프런- 데이터 분석을 위한 고급 SQL: 섹션6 - 정규표현식 문제풀이(틀림)

르네·2023년 10월 2일
0

SQL

목록 보기
36/63

인프런 강의 <데이터 분석을 위한 고급 SQL>을 듣고, 중요한 점을 정리한 글입니다.

문제 : Weather Observation Station 7

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

Input Format

The STATION table is described as follows:

풀이

  • 나의 풀이
SELECT DISTINCT city
FROM station
WHERE city REGEXP '.^*[aeiou]$'

: 이렇게 풀어도 정답으로 패스된다. 선생님 풀이와 조금 다른 문법이다.

  • 선생님 풀이
SELECT DISTINCT city
FROM station
WHERE city REGEXP '.*[aeiou]$'

배운점

  • [abc] : Only a, b, or c
  • . : Any Character
  • ^…$ : Starts and ends
  • * : Zero or more repetitions

문제 (틀림) : Weather Observation Station 8

Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. 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.

풀이

  • 나의 풀이(틀림)
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^.*[aeiou].*$'

: 정규표현식 테스트 사이트에서는 이렇게 테스트했을 때, 통과되는데 해커랭크에서는 틀렸다고 나온다.

  • 선생님 풀이
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou].*[aeiou]$'

배운점

  • city name의 첫 문자열과 마지막 문자열 각각을 표현해줘야 한다. 그러면 [aeiou]를 2번 써줘야 한다.

문제 : Weather Observation Station 9

Query the list of CITY names from STATION that do not start 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.

풀이

  • 나의 풀이
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[^aeiou]'

: 이렇게 표현할 수도 있다. 해커랭크에서 정답처리된다.

  • 선생님 풀이
SELECT DISTINCT city
FROM station
WHERE city NOT REGEXP '^[aeiou].*'

배운점

  • [^abc] : Not a, b, nor c
  • 선생님의 풀이를 보면, Weather Observation Station 6 문제와 반대 조건이니까 그 답의 반대로 표현하면 된다. REGEXP 앞에 NOT만 붙이면 된다.
profile
데이터분석 공부로그

0개의 댓글