사용한 키워드
- SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, COUNT, SUM, AVG, MAX, MIN, FLOOR, ROUND, TRUNCATE, DESC, LIMIT
1. Type of Triangle
Type of Triangle | HackerRank
Query a triangle's type based on its side lengths.
www.hackerrank.com
SELECT CASE
WHEN a=b AND b=c THEN 'Equilateral'
WHEN a+b <= c OR b+c <= a OR a+c <= b THEN 'Not A Triangle'
WHEN a=b OR b=c OR a=c THEN 'Isosceles'
ELSE 'Scalene'
END
FROM triangles;
CASE WHEN 조건문(여러 조건 사용하기)
CASE
WHEN 조건1 THEN 조건1을 만족할 때의 값
WHEN 조건2 THEN 조건2를 만족할 때의 값
ELSE 그 외의 조건을 만족할 때의 값
END
※ CASE문은 위에서부터 순차적으로 적용되는데 조건을 만족하면 뒤의 조건을 만족하더라도 선행되는 조건을 참으로 채택하고 다음 행으로 넘어간다. 따라서 순서에 유의하여 쿼리를 작성해야 한다.
- 세 변의 길이가 모두 같은 삼각형: Equilateral
a=b AND b=c
- 두 변의 길이가 같은 삼각형: Isosceles
a=b OR b=c OR a=c
- 세 변의 길이가 모두 다른 삼각형: Scalene
- 세 변의 길이가 삼각형의 성립 조건에 맞지 않는 경우: Not A Triangle
a+b <= c OR b+c <= a OR a+c <= b
2. Revising Aggregations - The Count Function
Revising Aggregations - The Count Function | HackerRank
Query the number of cities having populations larger than 100000.
www.hackerrank.com
SELECT COUNT(*)
FROM city
WHERE population > 100000 ;
3. Revising Aggregations - The Sum Function
Revising Aggregations - The Sum Function | HackerRank
Query the total population of all cities for in the District of California.
www.hackerrank.com
SELECT SUM(population)
FROM city
GROUP BY district
HAVING district = 'california';
4. Revising Aggregations - Averages
Revising Aggregations - Averages | HackerRank
Query the average population of all cities in the District of California.
www.hackerrank.com
SELECT AVG(population)
FROM city
GROUP BY district
HAVING district = 'california';
5. Average Population
Average Population | HackerRank
Query the average population of all cities, rounded down to the nearest integer.
www.hackerrank.com
SELECT FLOOR(AVG(population))
FROM city;
숫자 함수
FLOOR(숫자):
소수점 이하 버림
ex) FLOOR(13.7) -> 13
6. Japan Population
Japan Population | HackerRank
Query to the sum of the populations of all Japanese cities in CITY.
www.hackerrank.com
SELECT SUM(population)
FROM city
WHERE countrycode = 'JPN';
7. Population Density Difference
Population Density Difference | HackerRank
Query the difference between the maximum and minimum city populations in CITY.
www.hackerrank.com
SELECT MAX(population) - MIN(population)
FROM city;
8. The Blunder
The Blunder | HackerRank
Query the amount of error in Sam's result, rounded up to the next integer.
www.hackerrank.com
SELECT CEIL(AVG(salary) - AVG(REPLACE(salary, 0, '')))
FROM employees;
- 숫자 함수
CEIL(숫자): 소수점 자리를 무조건 올림
ex) CEIL(89.5) -> 90
- 문자 함수
REPLACE('문자열', '기존 문자열', '변경문자열')
ex) REPLACE
9. Top Earners
Top Earners | HackerRank
Find the maximum amount of money earned by any employee, as well as the number of top earners (people who have earned this amount).
www.hackerrank.com
SELECT salary*months AS earnings, COUNT(*)
FROM employee
GROUP BY earnings
ORDER BY earnings DESC
LIMIT 1;
10. Weather Observation Station 2
Weather Observation Station 2 | HackerRank
Write a query to print the sum of LAT_N and the sum of LONG_W separated by space, rounded to 2 decimal places.
www.hackerrank.com
SELECT ROUND(SUM(lat_n),2), ROUND(SUM(long_w),2)
FROM station;
숫자 함수
ROUND(숫자, 자릿수): 반올림
ex) ROUND(13.567, 2) -> 13.57
'스터디 > SQL' 카테고리의 다른 글
[SQL 연습 문제] 프로그래머스 SQL 고득점 키트 어려웠던 문제 풀이 (0) | 2022.08.25 |
---|---|
[SQL 연습 문제] 코딩 테스트 대비 해커랭크 easy 문제 풀이(4) (0) | 2022.07.26 |
[SQL 연습 문제] 코딩 테스트 대비 해커랭크 easy 문제 풀이(2) (0) | 2022.07.22 |
[SQL 연습 문제] 코딩 테스트 대비 해커랭크 easy 문제 풀이(1) (0) | 2022.07.21 |
[SQL 연습 문제] 프로그래머스 SQL 고득점 키트 문제 풀이(JOIN) (0) | 2022.04.29 |