본문 바로가기

SQL25

[LeetCode] Count Salary Categories https://leetcode.com/problems/count-salary-categories/description/ Union All을 잊지 말자. 그리고, select에 ''를 걸어 아예 필요한 문자열을 추출해버릴 수 있더라. WITH COUNT_SAL AS( SELECT 'Low Salary' AS 'category', COUNT(*) AS 'accounts_count' FROM ACCOUNTS WHERE INCOME < 20000 UNION ALL SELECT 'Average Salary' AS 'category', COUNT(*) AS 'accounts_count' FROM ACCOUNTS WHERE INCOME BETWEEN 20000 AND 50000 UNION ALL SELECT 'Hig.. 2024. 2. 6.
[LeetCode] Friend Requests II https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 처음의 실수는 join으로 잡았는데, 한 쪽 테이블에 정보가 없을 거라는 생각을 하지 못했다. join으로 풀 수야 있긴 한데, 효율적인 풀이는 아닐 것이다. Union all로 .. 2024. 1. 31.
[Leetcode] Confirmation Rate https://leetcode.com/problems/confirmation-rate/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Left join을 "현명하게" 활용해야 하는 문제, 그 이유는 없는 케이스를 만들어야 하니까. Confirmation이 1, 아니면 0을 평균으로 계산하는 아이디어는 덤. 이러면 NULL은 0만 줄거고, 0의 평균은 0이다... 2024. 1. 30.
[LeetCode] Department Top Three Salaries https://leetcode.com/problems/department-top-three-salaries/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 각 부서별로 동률을 고려하여 부서별 top3 연봉에 해당하는 부서, 직원, 급여을 출력하는 문제이다. DENSE_RANK를 알면 끝. SELECT DEPARTMENT, EMPLOYEE, SALARY FRO.. 2024. 1. 30.
[LeetCode] Second Highest Salary Problem : https://leetcode.com/problems/second-highest-salary/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com MAX가 아닌, 두번째로 높은 거를 찾는 건데, 그렇게 만만한 문제는 아니다! Sol 1) Dense_Rank를 이용한 풀이법 이게 동률일 경우도 고려해야 하므로, Rank를 매겨 2등을 찾고, 2등도.. 2024. 1. 30.
[SQL] 윈도우 함수 다시 정리 * 한번 더 내용을 다시 정리합니다. 생각보다 자주 쓰이겠더라고요. 윈도우 함수란? 행간의 관계를 정의할 수 있는 함수들입니다. 가능한 것들 : 랭킹 매기기, 누적합 계산하기 등등... 1) 누적합계 계산하기 SELECT EMPNO, ENAME, SAL SUM(SAL) OVER(ORDER BY SAL ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)TOTSAL FROM EMP; OVER를 활용합니다. OVER은 기본적으로 범위를 결정합니다. SAL을 정렬한 뒤, 제한없이 맨 앞부터 해당 줄 까지 입니다. 즉 위의 구문은 급여가 가장 높은 사람부터 순차대로 급여의 누적합을 뱉어줍니다. 여기서 UNBOUNDED 대신 숫자를 넣는다면, 원하는 행 개수 만큼만 위로 올라가.. 2024. 1. 27.