문제 사이트
아래 문제는 해커랭크에서 추출해온 문제입니다. 문제의 저작권은 해커랭크에 있으며 문제를 풀어보시려면 아래 링크를 클릭해주세요 🙂
Weather Observation Station 7 | HackerRank
Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION.
www.hackerrank.com
👉문제 설명

- 모음으로 끝나는 city 이름 출력, 중복값 없어야 함
👉문제 풀이
1. city 이름을 출력하되 모음으로 끝나야 함
2. 중복값이 없어야 함
👉문제 풀이 과정
1. city 이름 출력, 모음으로 끝
select city
from station
where city like '%a'
or city like '%e'
or city like '%i'
or city like '%o'
or city like '%u'
- like와 %를 이용하여 끝나야 하는 첫 글자를 지정해줌
2. 중복값 없어야 함
select distinct city
from station
where city like '%a'
or city like '%e'
or city like '%i'
or city like '%o'
or city like '%u'
- 중복값을 없애는 distinct 함수 사용
- 중복되는 값이 따로 없기 때문에 distinct 함수를 안 써도 똑같은 결과가 나오지만 혹시 모를 상황에 대비해 써줘야 한다
👉최종 답안
select distinct(city)
from station
where city like 'a%'
or city like 'e%'
or city like 'i%'
or city like 'o%'
or city like 'u%'
++ 추가적인 방법
다른 사람들 풀이도 참고하고 있는데 나와는 다르게 간결하게 푸신 분들이 많았다
보고 또 배웁니다,,
1) 정규표현식을 활용하여 문제 풀기
select distinct city
from station
where city REGEXP '[aeiou]$'
2) 뒤에 글자 하나만 빼내서 비교하는 방법
substr(data, i) : i번째 자리에 있는 문자열 추출
upper(data) : 대문자로 바꿔줌
select distinct city
from station
where upper(substr(city, -1)) in ('A', 'E', 'I', 'O', 'U')
👉고찰
저번에 풀었던 모음으로 시작하는 단어 출력하는 것과 똑같은 문제이다. 모음으로 시작하냐 끝나냐의 차이일뿐
'SQL > MySQL' 카테고리의 다른 글
[MySQL] HackerRank - New Companies 문제 풀이 (0) | 2025.03.12 |
---|---|
[MySQL] HackerRank - Weather Observation Station 8 문제 풀이 (0) | 2025.02.10 |
[MySQL] HackerRank - Weather Observation Station 6 문제 풀이 (0) | 2025.01.31 |
[MySQL] HackerRank - Type of Triangle 문제 풀이 / CASE WHEN (0) | 2024.10.18 |
[MySQL] HackerRank - Binary Tree Nodes 문제 풀이 (0) | 2024.08.31 |