본문 바로가기
DB/SQL

[해커랭크] Easy 문제 풀이 1

by h_sseul 2023. 7. 20.

 

# Revising the Select Query |

select *
from city
where population > 100000 and countrycode = 'USA';

 

# Revising the Select Query ||

 

select name
from city
where population > 120000 and countrycode = 'USA';

 

# Select All

 

select *
from city;

 

# Select by ID

 

select *
from city
where id = '1661';

 

# Japanese Cities' Attributes

 

select *
from city
where countrycode = 'JPN';

 

# Japanese Cities' Names

 

select name
from city
where countrycode = 'JPN';

 

# Weather Observation Station 1

 

select city, state
from station;

 

# Weather Observation Station 3

 

select distinct city
from station
where id % 2 = 0;

 

# Weather Observation Station 4

 

select count(city) - count(distinct city)
from station;

 

# Weather Observation Station 5

 

city 길이가 가장 짧은 도시와 가장 긴 도시 출력. 이때 길이가 같다면 알파벳 순으로.

 

(select city, length(city)
from station
order by length(city), city
limit 1)

union

(select city, length(city)
from station
order by length(city) desc, city
limit 1)

 

# Weather Observation Station 6

 

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

 

# Weather Observation Station 7

 

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

 

# Weather Observation Station 8

 

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

 

정규표현식(REGEXP) 기호

. : 문자 하나를 나타냄
* : 앞에 나온 문자의 0개 이상 반복을 나타냄
^ : 문자열의 처음을 나타냄
$ : 문자열의 끝을 나타냄
[.] : 괄호 안의 문자열 일치 확인
{.} : 반복을 나타냄
[^] : 괄호 안에 있는 문자열 제외
| : or 을 나타냄

 

# Weather Observation Station 9

 

select distinct city
from station
where left(city, 1) not in ('a', 'e', 'i', 'o', 'u');
-- where city regexp '^[^aeiou]'

 

# Weather Observation Station 10

 

select distinct city
from station
where city regexp '[^aeiou]$';

 

# Weather Observation Station 11

select distinct city
from station
where city regexp '^[^aeiou]' or city regexp '[^aeiou]$';

 

# Weather Observation Station 12

 

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

 

평소에 정규표현식 자주 안 써봤는데 관련된 문제가 많아 짚고 넘어갈 수 있어 좋았다. 활용도가 높은만큼 더 익숙해지자!

 

# Employee Names

 

select name
from employee
order by name;

 

# Employee Salaries

 

select name
from employee
where salary > 2000 and months < 10
order by employee_id;

 

 

 

'DB > SQL' 카테고리의 다른 글

[solvesql] 문제 정리  (0) 2023.07.19
[SQL] Window Function 정리  (0) 2023.06.08
[SQL] WITH절 정리  (0) 2023.06.08
[solvesql] 연습 문제 (난이도 : 쉬움)  (0) 2023.05.23
[Programmers] String, Date 문제 풀이  (0) 2023.04.07