SQL 코딩테스트 - Weather Observation Station 8(★★/▲/1)

기운찬곰·2021년 3월 27일
0

SQL

목록 보기
1/2

💻 주소 : https://www.hackerrank.com/challenges/weather-observation-station-8/problem


문제

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:


해결방법

문제 이해하기

문제를 해석해보자면 CITY 이름 중에서 맨앞과 맨뒤과 e, a, e, i, o, u 중에 하나여야 한다는 뜻이다.

문자열 함수 이용

뭐 해결 방법으로는 나처럼 left, right를 사용하거나 substr을 이용해서 그 부분만 문자를 추출해서 비교하는 방식이 있을 수 있겠다.

정규표현식 이용

두번째 방식으로는 간단하게 정규표현식을 이용하면 된다.


소스코드

내 코드

select distinct(city)
from station
where left(city, 1) in ('a', 'e', 'i', 'o', 'u') and right(city, 1) in ('a', 'e', 'i', 'o', 'u')

추천 코드

select distinct(city)
from station
where city regexp '^[aeiou]' and city regexp '[aeiou]$'

정규 표현식을 사용하는게 더 깔끔하고 가독성이 좋아보인다.

profile
velog ckstn0777 부계정 블로그 입니다. 프론트 개발 이외의 공부 내용을 기록합니다. 취업준비 공부 내용 정리도 합니다.

0개의 댓글