최근 글 ✨

Study/SQL

[LeetCode] 570. Managers with at Least 5 Direct Reports

문제Write a solution to find managers with at least five direct reports.Return the result table in any order.The result format is in the following example. 쿼리SELECT NAMEFROM EMPLOYEEWHERE ID IN ( SELECT MANAGERID FROM EMPLOYEE GROUP BY MANAGERID HAVING COUNT(MANAGERID) >= 5); 제출하고 보니 생각보다 실행 시간이 길게 나왔다. 생각해보니 그냥 조인만 써서도 풀 수 있을 것 같아서 쿼리를 다시 작성했다 SELECT E.NAME..

[LeetCode] 97. Rising Temperature

문제Write a solution to find all dates' id with higher temperatures compared to its previous dates (yesterday).Return the result table in any order.The result format is in the following example. 전날보다 온도가 높은 날의 id값을 출력하면 된다. 쿼리 #1SELECT a.id AS IdFROM Weather aJOIN Weather b ON DATEDIFF(a.recordDate, b.recordDate) = 1 AND a.temperature > b.temperature; 쿼리 #2(LAG 활용)SELECT idFROM ( SELECT ..

[LeetCode] 196. Delete Duplicate Emails

문제Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one.For Pandas users, please note that you are supposed to modify Person in place.After running your script, the answer shown is the Person table. The driver will first compile and run your piece of cod..

[LeetCode] 185. Department Top Three Salaries

문제A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.Write a solution to find the employees who are high earners in each of the departments.Return the result table in any order.The result format is in the following example...

[LeetCode] 184. Department Highest Salary

문제Write a solution to find employees who have the highest salary in each of the departments.Return the result table in any order.The result format is in the following example. 쿼리SELECT B.NAME AS DEPARTMENT, A.NAME AS EMPLOYEE, A.SALARY AS SALARYFROM EMPLOYEE ALEFT JOIN DEPARTMENT B ON A.DEPARTMENTID = B.IDWHERE (A.DEPARTMENTID, A.SALARY) IN ( SELECT ..

[LeetCode] 183. Customers Who Never Order

문제Write a solution to find all customers who never order anything.Return the result table in any order.The result format is in the following example. 아무것도 주문하지 않은 사용자를 출력하면 된다. 쿼리 #1 NOT INSELECT NAME AS CUSTOMERSFROM CUSTOMERSWHERE ID NOT IN ( SELECT CUSTOMERID FROM ORDERS ); #2 JOIN + IS NULLSELECT A.NAME AS CUSTOMERSFROM CUSTOMERS A..

[LeetCode] 182. Duplicate Emails

문제Write a solution to report all the duplicate emails. Note that it's guaranteed that the email field is not NULL.Return the result table in any order.The result format is in the following example. 중복되는 이메일이 있으면 해당 이메일을 출력하도록 해야한다. 쿼리SELECT EMAILFROM PERSONGROUP BY EMAILHAVING COUNT(EMAIL) > 1; 문제 출처https://leetcode.com/problems/duplicate-emails/description/

[LeetCode] 180. Consecutive Numbers

문제Find all numbers that appear at least three times consecutively.Return the result table in any order.The result format is in the following example. 적어도 3번 연속되는 숫자를 출력하면 된다. 출력 쿼리SELECT DISTINCT A.NUM AS ConsecutiveNumsFROM LOGS AJOIN LOGS B ON A.ID + 1 = B.ID AND A.NUM = B.NUMJOIN LOGS C ON A.ID + 2 = C.ID AND A.NUM = C.NUM; SELECT DISTINCT A.NUM AS Consecut..

[LeetCode] 178. Rank Scores

문제Write a solution to find the rank of the scores. The ranking should be calculated according to the following rules: The scores should be ranked from the highest to the lowest.If there is a tie between two scores, both should have the same ranking.After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks.Return the ..

[LeetCode] 177. Nth Highest Salary

문제Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null. Employee 테이블에서 n번째로 높은 연봉을 출력한다. 없으면 null을 출력한다. 쿼리CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INTBEGINSET N=N-1; RETURN ( SELECT DISTINCT SALARY FROM EMPLOYEE ORDER BY SALARY DESC LIMIT N,1 );END null 처리를 위해서 Distinct를 사용해줬고 하나만 출력하면 돼서 limit를..