스터디/SQL

[SQL 연습 문제] 코딩 테스트 대비 해커랭크 easy 문제 풀이(2)

skyggg3 2022. 7. 22. 23:33

사용한 키워드

- SELECT, FROM, DISTINCT, WHERE, LIKE, LEFT, RIGHT, IN, NOT IN, ORDER BY, CASE WHEN 

 

 

1.  Weather Observation Station 6

 

Weather Observation Station 6 | HackerRank

Query a list of CITY names beginning with vowels (a, e, i, o, u).

www.hackerrank.com

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%'
SELECT DISTINCT city
FROM station
WHERE LEFT(city,1) IN ('a','e','i','o','u');
SELECT DISTINCT city
FROM station
WHERE city REGEXP '^[aeiou].*';

정규표현식 사용

REGEXP (패턴)

ex) REGEXP('^[aeiou].*')

^:시작한다 / [aeiou]: [__] 목록에 있는 것중에 하나로 / .*: %와 비슷한 의미(0개 이상의 어떤 문자열이 와도 상관없음)

 

 

2. Weather Observation Station 7

 

Weather Observation Station 7 | HackerRank

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION.

www.hackerrank.com

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';

 

 

3. Weather Observation Station 8

 

Weather Observation Station 8 | HackerRank

Query CITY names that start AND end with vowels.

www.hackerrank.com

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');

문자열 자르기

- LEFT(문자열, 길이)

- RIGHT(문자열, 길이)

 

 

4. Weather Observation Station 9

 

Weather Observation Station 9 | HackerRank

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

www.hackerrank.com

SELECT DISTINCT city
FROM station 
WHERE LEFT(city,1) NOT IN ('a','e', 'i', 'o','u');

 

 

5. Weather Observation Station 10

 

Weather Observation Station 10 | HackerRank

Query a list of CITY names not ending in vowels.

www.hackerrank.com

SELECT DISTINCT city
FROM station 
WHERE RIGHT(city,1) NOT IN ('a','e', 'i', 'o','u');

 

 

6. Weather Observation Station 11

 

Weather Observation Station 11 | HackerRank

Query a list of CITY names not starting or ending with vowels.

www.hackerrank.com

SELECT DISTINCT city
FROM station 
WHERE LEFT(city,1) NOT IN ('a','e', 'i', 'o','u')
OR RIGHT(city,1) NOT IN ('a','e', 'i', 'o','u');

 

 

7. Weather Observation Station 12

 

Weather Observation Station 12 | HackerRank

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

www.hackerrank.com

SELECT DISTINCT city
FROM station 
WHERE LEFT(city,1) NOT IN ('a','e', 'i', 'o','u')
AND RIGHT(city,1) NOT IN ('a','e', 'i', 'o','u');

 

 

8. Higher Than 75 Marks

 

Higher Than 75 Marks | HackerRank

Query the names of students scoring higher than 75 Marks. Sort the output by the LAST three characters of each name.

www.hackerrank.com

SELECT name
FROM students
WHERE marks > 75
ORDER BY RIGHT(name,3), id;

 

 

9. Employee Names

 

Employee Names | HackerRank

Print employee names.

www.hackerrank.com

SELECT name
FROM employee
ORDER BY name;

 

 

10. Employee Salaries

 

Employee Salaries | HackerRank

Print the names of employees who earn more than $2000 per month and have worked at the company for less than 10 months.

www.hackerrank.com

SELECT name
FROM employee
WHERE salary > 2000 
AND months < 10
ORDER BY employee_id;