[해커랭크] Weather Observation Station 6 / 7 / 8 / 9

june·2023년 3월 19일
0

SQL

목록 보기
4/31

Weather Observation Station 6

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

  • Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
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%'

Lesson & Learned

패턴을 찾을 때는 WHERE절에 LIKE 연산을 사용한다.
자주 사용되는 와일드카드

  • %(percent) : zero, one, or multiple characters
  • _(underscore) : one, single character

Weather Observation Station 7

https://www.hackerrank.com/challenges/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.
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'

Lesson & Learned

🔁 정규표현식으로 접근해보기(6~11)

Weather Observation Station 8

https://www.hackerrank.com/challenges/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.
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou].*[aeiou]$'

Lesson & Learned

  1. ⭐ 정규표현식
  • Starting and ending : ^ and $
  • [abc] : only a, b, c
  • . : any character
  • * : 0 or more repetitions
  1. 다른 풀이
    LEFT(string, number_of_chars) / RIGHT( ) 함수 사용
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')

Weather Observation Station 9

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

  • Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
SELECT DISTINCT city
FROM station
WHERE city NOT LIKE 'a%'
    AND city NOT LIKE 'e%'
    AND city NOT LIKE 'i%'
    AND city NOT LIKE 'o%'
    AND city NOT LIKE 'u%'

Lesson & Learned

7번문제와 비교. 조건에서 AND / OR 생각하기

profile
나의 계절은

0개의 댓글