본문 바로가기

Algorithm

(131)
[해커랭크(HackerRank)] The Blunder (MySQL) 문제 코드 SELECT CEIL(AVG(salary) - AVG(replace(salary,0,''))) FROM employees 모든 직원들의 월급 평균을 구하라 그러나 사만다의 키보드 숫자 0이 고장나서 0이 없을 때와 있을 때의 오차를 구하고자 함 올림해서 보여줘야 하기 때문에 0이 있는 원본 데이터에서 0이 없는 데이터를 빼서 평균 값을 구한 다음 CEIL() 함수 사용 출처
[해커랭크(HackerRank)] Japan Population (MySQL) 문제 Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN. Input Format The CITY table is described as follows: 코드 SELECT sum(population) FROM City WHERE countrycode = 'JPN' city 테이블에서 모든 일본 도시의 인구 합계를 출력하라 일본의 country code = 'JPN' 출처
[해커랭크(HackerRank)] Symmetric Pairs (MySQL) 문제 ou are given a table, Functions, containing two columns: X and Y. Two pairs (X1, Y1) and (X2, Y2) are said to be symmetric pairs if X1 = Y2 and X2 = Y1. Write a query to output all such symmetric pairs in ascending order by the value of X. List the rows such that X1 ≤ Y1. 코드 # X와 Y가 같지 않은 것과 같은 것을 UNION으로 묶어서 보여주기 # X와 Y가 같지 않을 때 SELECT A.X, A.Y FROM Functions A INNER JOIN Functions B ON A.X ..
[LeetCode] 197. Rising Temperature (MySQL) 문제 코드 SELECT today.id as id FROM Weather as today INNER JOIN Weather as yesterday ON yesterday.recordDate = date_add(today.recordDate, interval -1 day) WHERE yesterday.temperature < today.temperature 이전 날짜(어제)에 비해 온도가 높은 모든 날짜의 ID 찾기 먼저 today 테이블과 yesterday테이블의 조인 조건을 찾는 게 관건 순서가 any order라서 join 조건을 날짜로 줘야한다 오늘의 온도가 어제보다 더 높은 데이터만 출력 어제의 날짜는 today.recordDate에서 하루를 뺀 값 출처