본문 바로가기

Algorithm

(131)
[LeetCode] 185. Department Top Three Salaries (MySQL) 문제 부서 별 고소득자 구하기 해당 부서의 top 3에 해당하는 급여를 받는 직원 구하기 코드 1. 부서 별 고소득자를 구해야하기 때문에 순위 정하는 함수 DENSE_RANK() 사용 top3에 해당하는 직원이 아니라 top3 급여를 받는 직원을 구하면 되기 때문에 DENSE_RANK()를 사용했다. DENSE_RANK()와 RANK() 차이점은 RANK()는 2명이 90000이고 90000이 max값이면 둘 다 1위가 되고 다음 순위가 3위가 되는데 DENSE_RANK()는 1위가 2번이면 둘 다 1위가 되고 그 다음 순위는 2위가 된다. SELECT departmentId , name , salary , DENSE_RANK() OVER (partition by departmentId ORDER BY ..
[LeetCode] 180. Consecutive Numbers (MySQL) 문제 연속해서 세 번 이상 나타나는 모든 숫자를 찾아라 순서 상관없음 코드 Join select distinct a.num as ConsecutiveNums from logs a inner join logs b on a.id = b.id+1 inner join logs c on a.id = c.id+2 where a.num = b.num and a.num = c.num 너무 하드 코딩인 것 같다.. 처음에 join 조건을 between 으로 주려다가 num만 값을 빼오는 방법을 모르겠어서 에라 모르겠다 하고 했는데 통과해서 어안이 벙벙함 Lead select distinct sub.num as ConsecutiveNums from ( select id , num , lead(num, 1) over (or..
[LeetCode] 184. Department Highest Salary (MySQL) 문제 employee테이블은 모든 직원들을 가지고 있다 모든 직원들은 id, salary, departmentId를 갖고있다 Department테이블은 회사의 모든 부서들을 갖고있다 각각의 부서에서 가장 높은 월급을 받는 직원들을 조회해라 코드 INNER JOIN + SUBQUERY 1. 직원별 각 부서 이름이 필요하므로 Employee 테이블과 Department 테이블 조인 SELECT * FROM Employee e INNER JOIN Department d ON e.DepartmentId = d.Id 2. group by를 해줘서 부서별 max 값 확인하기 SELECT d.name as Department, MAX(e.salary) as Salary FROM Employee e INNER JOIN..
[해커랭크(HackerRank)] The Report (MySQL) 문제 You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks. Grades contains the following data: Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered firs..