본문 바로가기

분류 전체보기267

[LeetCode] Delete Duplicate Emails (delete, self join, and) Delete 문을 사용해서 테이블에서 행 제거 DELETE p FROM Person p INNER JOIN Person p2 ON p.Email = p2.Email AND p.Id > p2.Id; 궁금증이 생기는데 on 절에 조건을 줄 경우 어떤식으로 영향이 미치는가 위의 코드에서는 and라는 조건이 추가된 것이다. [ON vs WHERE] ON : JOIN 을 하기 전 필터링을 한다 (=ON 조건으로 필터링이 된 레코들간 JOIN이 이뤄진다) WHERE : JOIN 을 한 후 필터링을 한다 (=JOIN을 한 결과에서 WHERE 조건절로 필터링이 이뤄진다) 출처: developyo.tistory.com/121 다른 풀이 With cte as (select min(Id) as Id, Email from P.. 2021. 4. 9.
[LeetCode] Customes Who Never Order (is null, join) 기본적으로 Customers join Orders 구조 select * from customers c join orders o on c.ID = o.CustomerID 이 상태로 출력해보면 "headers": ["Id", "Name", "Id", "CustomerId"], "values": [[1, "Joe", 2, 1], [3, "Sam", 1, 3]] 이렇게 나온다. 따라서 left join이 필요하다. select * from customers c left join orders o on c.ID = o.CustomerID left join의 결과는 {"headers": ["Id", "Name", "Id", "CustomerId"], "values": [[1, "Joe", 2, 1], [2, "Hen.. 2021. 4. 9.
[LeetCode] Duplicate Emails (group by, having) Having: Pandas 의 agg select email from person group by email having count(email) > 1 ; Having 절 출처: keep-cool.tistory.com/37 HAVING 절은 해석상 WHERE 절과 동일하다. 단 조건 내용에 그룹 함수를 포함하는 것만을 포함한다. 일반 조건은 WHERE 절에 기술하지만 그룹 함수를 포함한 조건은 HAVING 절에 기술한다. having은 group by로 묶인 부분에 대해서 조건을 수행한다는 것. Pandas 의 agg로 생각하면 된다. 2021. 4. 9.
[LeetCode] Employees Earning More Than Their Managers (Self Join) select e.Name as Employee from Employee as e join Employee as m on m.Id = e.ManagerId where e.Salary > m.Salary 위 코드에서 e는 Joe, Henry 만 남아있고(join의 결과 managerID == ID) m은 Sam, Max만 존재하는 것이다. self join www.w3schools.com/sql/sql_join_self.asp SQL Self Join SQL Self Join SQL Self Join A self join is a regular join, but the table is joined with itself. Self Join Syntax SELECT column_name(s) FROM table.. 2021. 4. 8.
[LeetCode] Second Highest Salary MySQL # Write your MySQL query statement below SELECT MAX(Salary) as SecondHighestSalary FROM Employee WHERE Salary NOT IN (SELECT Max(Salary) FROM Employee); NOT IN 으로 max salary를 찾으면 이것이 1st highest salary이기 때문에 이 값을 제외한 나머지 중에서 가장 큰 값을 "SecondHighestSalary"로 출력 출처: www.geeksforgeeks.org/query-to-find-2nd-largest-value-in-a-column-in-table/ 2021. 4. 8.
[MySQL] Code Snippets 최댓값 구하기 2가지 방식 SELECT DATETIME AS 시간 FROM ANIMAL_INS ORDER BY DATETIME DESC LIMIT 1 SELECT MAX(DATETIME) AS '시간' FROM ANIMAL_INS 최솟값 구하기 SELECT DATETIME AS 시간 FROM ANIMAL_INS ORDER BY DATETIME ASC LIMIT 1 COUNT SELECT COUNT(*) FROM ANIMAL_INS # COUNT(*)으로 행의 개수 구하기 DUPLICATE (DISTINCT 또는 GROUPBY로 사용 가능) SELECT COUNT(*) FROM ANIMAL_INS # COUNT(*)으로 행의 개수 구하기 GROUP BY ORDER BY SELECT ANIMAL_TYPE, .. 2021. 4. 8.
[LeetCode] Binary Tree Paths 파이썬 (Binary Tree, LCA, DFS) class Solution(object): # @param {TreeNode} root # @return {string[]} def binaryTreePaths(self, root): result, path = [], [] self.binaryTreePathsRecu(root, path, result) return result def binaryTreePathsRecu(self, node, path, result): if node is None: return if node.left is node.right is None: ans = "" for n in path: ans += str(n.val) + "->" result.append(ans + str(node.val)) if node.left: path... 2021. 4. 7.
[LeetCode] Valid Anagram 파이썬 (dict) class Solution: def isAnagram(self, s: str, t: str) -> bool: def get_dict(word): res = {} for c in word: if c in res: res[c] += 1 else: res[c] = 1 return sorted(res.items()) sd = get_dict(s) td = get_dict(t) return sd == td dict를 sort 하려면 sorted 안에 dict.items()를 넣어주면 된다. 아래 처럼 key를 사용해서 key, value 순으로 sort가능하다. ygseo.tistory.com/215 2021. 4. 7.
[LeetCode] Delete Node in a Linked List 파이썬 (linked list) class Solution: def deleteNode(self, node): if node and node.next: node.val = node.next.val # swap current val to next val node.next = node.next.next # jump next to next.next 2021. 4. 7.