Algorithm/SQL Query test

[MySQL] HackerRank - Weather Observation Station 9

감자 🥔 2021. 7. 22. 21:44
반응형

문제

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

 

Weather Observation Station 9 | HackerRank

Query an alphabetically ordered list of CITY names not starting with vowels.

www.hackerrank.com

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 NOT LIKE 'A%'
AND CITY NOT LIKE 'E%'
AND CITY NOT LIKE 'I%'
AND CITY NOT LIKE 'O%'
AND CITY NOT LIKE 'U%'

여기서 AND 가 아니라 OR을 써서 시간을 뺏겼다. NOT LIKE니까 AND로 연결해주는게 맞다.... A로 시작하지도 않고, E로 시작하지도 않고... 이렇게 되는 거니까!

이제 앞에서 배웠던 정규식을 활용해서 다시 풀어보겠다.

SELECT DISTINCT CITY
FROM STATION
WHERE CITY NOT REGEXP '^[aeiou]'

-- ^는 시작을 의미!! AEIOU로 시작하지 않는 것을 골라라

반응형