https://leetcode.com/problems/consecutive-numbers/
row 3개 연속해서 등장하는 num을 찾는 문제.
1. Where IN 활용
select distinct Num as ConsecutiveNums from Logs
where (Id+1,Num) in (Select * from Logs) and (ID+2, Num) in (Select * from Logs)
;
2. self join 활용
SELECT distinct a.num ConsecutiveNums FROM
Logs a JOIN Logs b
on a.id = b.id - 1
JOIN logs c
on b.id = c.id - 1
WHERE a.num = b.num and b.num = c.num
;
'Programming > LeetCode' 카테고리의 다른 글
[LeetCode] Duplicate Emails (0) | 2022.03.31 |
---|---|
[LeetCode] Employees Earning More Than Their Managers (0) | 2022.03.31 |
[LeetCode] Combine Two Tables (MySQL) (0) | 2022.03.31 |
[LeetCode] Search a 2D Matrix (0) | 2022.03.30 |
[LeetCode] Design Circular Queue (스택으로 원형 큐 구현) (0) | 2022.03.29 |
댓글