본문 바로가기

Algorithm/MySQL

(75)
[해커랭크(HackerRank)] New Companies (MySQL) 문제 Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy: Given the table schemas below, write a query to print the company_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company_code. Note: The tables may contain d..
[해커랭크(HackerRank)] Binary Tree Nodes (MySQL) 문제 2진 트리 문제. N은 이진 트리의 노드 값, P는 N의 상위 항목 Root → 최상위 노드인 경우, 부모가 없는 노드 → 5가 최상위 노드 Leaf → 최하위 노드인 경우, 자식이 없음 → 1, 3, 6, 9 → n이 p의 범위에 포함되지 않는 것들 Inner → root도 leaf도 아닌 경우 → 2, 8 각 조건에 해당하는 노드 출력 코드 1. subquery SELECT N, CASE WHEN P IS NULL THEN 'Root' WHEN N NOT IN (SElECT Distinct P FROM bst WHERE P IS NOT NULL) THEN 'Leaf' ELSE 'Inner' END FROM BST ORDER BY N 각 node에 대해서 parent의 case 따라 node ty..
[해커랭크(HackerRank)] Occupations (MySQL) 문제 행과 열을 바꾸는 pivot 활용 직업 아래에 이름이 표시되록 직업 열을 피벗 (Doctor, Prfessor, Singer, Actor 순으로) 이름은 알파벳 순으로 정렬 코드 #1. case문으로 확인하기 but 한줄만 출력됨 SELECT MIN(CASE WHEN OCCUPATION = 'DOCTOR' THEN NAME ELSE NULL END) , MIN(CASE WHEN OCCUPATION = 'PROFESSOR' THEN NAME ELSE NULL END) , MIN(CASE WHEN OCCUPATION = 'SINGER' THEN NAME ELSE NULL END) , MIN(CASE WHEN OCCUPATION = 'ACTOR' THEN NAME ELSE NULL END) FROM OC..
[LeetCode] 196. Delete Duplicate Emails (MySQL) 문제 코드 중복이 되는 이메일 삭제하되 ID가 작은 이메일 한개만 남기는 문제 DELETE JOIN DELETE A FROM Person A INNER JOIN Person B ON A.Email = B.Email WHERE A.id > B.id Subquery -- subquery type DELETE FROM Person WHERE Id NOT In (Write a subquery which only contains Ids should be deleted); # 1. keeping 해야하는 이메일 구하기 SELECT email, MIN(Id) FROM person GROUP BY email # 2. min_id만 가져오기 SELECT sub.min_id FROM ( SELECT email, MIN(I..