SQL/해커랭크
-
[문제] Top Earners | HackerRank 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 [문제 풀이] MONTHS * SALARY의 최댓값과 가장 많이 버는 사람이 복수라면 해당 사람의 숫자를 출력하라는 문제이다. MONTHS * SALARY 를 그룹으로 묶은 뒤에 order by 와 limit를 사용해서 풀어보도록 하자. GROUP BY를 이용해서 소득으로 사람들을 나눌 수 있으니 가장 많이 버는 그룹만 count를 통해서 몇..
[MySQL] Top Earners[문제] Top Earners | HackerRank 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 [문제 풀이] MONTHS * SALARY의 최댓값과 가장 많이 버는 사람이 복수라면 해당 사람의 숫자를 출력하라는 문제이다. MONTHS * SALARY 를 그룹으로 묶은 뒤에 order by 와 limit를 사용해서 풀어보도록 하자. GROUP BY를 이용해서 소득으로 사람들을 나눌 수 있으니 가장 많이 버는 그룹만 count를 통해서 몇..
2022.10.29 -
[문제] Ollivander's Inventory | HackerRank Ollivander's Inventory | HackerRank Help pick out Ron's new wand. www.hackerrank.com [문제 풀이] 문제가 요구하는 것을 정확히 파악하지 못해서 헤매어서 풀었던 문제. 문제에서 요구하는 것은 1. is_evil 은 0이어야 한다. 2. 만약 age와 power가 같다면 coins_needed가 작은 값만을 가져와야 한다. 1번은 바로 이해가 되었는데 2번을 바로 파악을 못해서 헤맸었다. JOIN을 쓰고 SELECT 부분에서 한번 더 JOIN 으로 power와 age가 일치하고 evil은 0인 최소 값의 coins_needed를 가져오자. 밑에서 A2.power = A..
[MySQL] Ollivander's Inventory[문제] Ollivander's Inventory | HackerRank Ollivander's Inventory | HackerRank Help pick out Ron's new wand. www.hackerrank.com [문제 풀이] 문제가 요구하는 것을 정확히 파악하지 못해서 헤매어서 풀었던 문제. 문제에서 요구하는 것은 1. is_evil 은 0이어야 한다. 2. 만약 age와 power가 같다면 coins_needed가 작은 값만을 가져와야 한다. 1번은 바로 이해가 되었는데 2번을 바로 파악을 못해서 헤맸었다. JOIN을 쓰고 SELECT 부분에서 한번 더 JOIN 으로 power와 age가 일치하고 evil은 0인 최소 값의 coins_needed를 가져오자. 밑에서 A2.power = A..
2022.10.29 -
[문제] The Report | HackerRank The Report | HackerRank Write a query to generate a report containing three columns: Name, Grade and Mark. www.hackerrank.com [문제 풀이] SQL에서 IF절에 대해서 배워보도록 하자. IF(조건, 참일때 값, false일 때 값) if문을 이용해서 GRADE가 8이상이면 name을 이하면 null을 출력해주자. [코드] SELECT IF(GRADE>=8,NAME,NULL), GRADE, MARKS FROM GRADES, STUDENTS WHERE MARKS >= MIN_MARK AND MARKS
[MySQL] The Report[문제] The Report | HackerRank The Report | HackerRank Write a query to generate a report containing three columns: Name, Grade and Mark. www.hackerrank.com [문제 풀이] SQL에서 IF절에 대해서 배워보도록 하자. IF(조건, 참일때 값, false일 때 값) if문을 이용해서 GRADE가 8이상이면 name을 이하면 null을 출력해주자. [코드] SELECT IF(GRADE>=8,NAME,NULL), GRADE, MARKS FROM GRADES, STUDENTS WHERE MARKS >= MIN_MARK AND MARKS
2022.10.25 -
[문제] Average Population of Each Continent | HackerRank Average Population of Each Continent | HackerRank Query the names of all continents and their respective city populations, rounded down to the nearest integer. www.hackerrank.com [문제 풀이] FLOOR와 ROUND의 차이 그리고 JOIN을 사용한 상태에서 GROUP BY에 관한 문제이다. ROUND는 반올림을 할 수 있는 함수이며 FLOOR는 내림을 하는 함수이다. 해당 문제에서는 가장 가까운 정수로 내려달라고 했으니 FLOOR를 사용하도록 하자. 또한, JOIN을 ..
[MySQL] Average Population of Each Continent[문제] Average Population of Each Continent | HackerRank Average Population of Each Continent | HackerRank Query the names of all continents and their respective city populations, rounded down to the nearest integer. www.hackerrank.com [문제 풀이] FLOOR와 ROUND의 차이 그리고 JOIN을 사용한 상태에서 GROUP BY에 관한 문제이다. ROUND는 반올림을 할 수 있는 함수이며 FLOOR는 내림을 하는 함수이다. 해당 문제에서는 가장 가까운 정수로 내려달라고 했으니 FLOOR를 사용하도록 하자. 또한, JOIN을 ..
2022.10.12 -
[문제] Population Census | HackerRank Population Census | HackerRank Query the sum of the populations of all cities on the continent 'Asia'. www.hackerrank.com [문제 풀이] 이제 JOIN에 대해서 알아볼 때가 왔다! 우선 JOIN을 사용하기 위하여 두 테이블을 연결하는 컬럼이 무엇인지 알아보면 Note: CITY.CountryCode and COUNTRY.Code are matching key columns. CITY.CountryCode 와 COUNTRY.Code가 일치하니 두개의 컬럼을 연결고리로 테이블을 연결시키자. JOIN 테이블 A(부를것) ON 연결고리 우리는 CITY를 ..
[MySQL] Population Census[문제] Population Census | HackerRank Population Census | HackerRank Query the sum of the populations of all cities on the continent 'Asia'. www.hackerrank.com [문제 풀이] 이제 JOIN에 대해서 알아볼 때가 왔다! 우선 JOIN을 사용하기 위하여 두 테이블을 연결하는 컬럼이 무엇인지 알아보면 Note: CITY.CountryCode and COUNTRY.Code are matching key columns. CITY.CountryCode 와 COUNTRY.Code가 일치하니 두개의 컬럼을 연결고리로 테이블을 연결시키자. JOIN 테이블 A(부를것) ON 연결고리 우리는 CITY를 ..
2022.10.11 -
[문제] Higher Than 75 Marks | HackerRank 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 [문제 풀이] 우측에서 3가지 문자를 파악해서 정렬하자. RIGHT(컬럼,숫자) 해당 구절은 컬럼의 우측 순서만큼을 뽑아 주는 구절이다. ORDER BY와 조합을 해서 문제를 풀어보자. [코드] SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY RIGHT(NAME,3) ASC, ID ASC
[MySQL] Higher Than 75 Marks[문제] Higher Than 75 Marks | HackerRank 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 [문제 풀이] 우측에서 3가지 문자를 파악해서 정렬하자. RIGHT(컬럼,숫자) 해당 구절은 컬럼의 우측 순서만큼을 뽑아 주는 구절이다. ORDER BY와 조합을 해서 문제를 풀어보자. [코드] SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY RIGHT(NAME,3) ASC, ID ASC
2022.10.10