-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sql
52 lines (50 loc) · 830 Bytes
/
main.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- use WHERE clause
-- solution1
SELECT
DISTINCT l1.num AS ConsecutiveNums
FROM
Logs l1,
Logs l2,
Logs l3
WHERE
l1.id = l2.id-1 AND
l2.id = l3.id-1 AND
l1.num = l2.num AND
l2.num = l3.num;
-- use JOIN
-- solution2
SELECT
DISTINCT l1.num AS ConsecutiveNums
FROM
Logs l1
JOIN
Logs l2
ON
l1.id = l2.id-1
JOIN
Logs l3
ON
l1.id = l3.id-2
WHERE
l1.num = l2.num AND l2.num = l3.num;
-- use EXISTS and subquery
-- solution3
SELECT
DISTINCT l1.num AS ConsecutiveNums
FROM
Logs l1
WHERE EXISTS (
SELECT
1
FROM
Logs l2
WHERE
l1.id+1 = l2.id AND l1.num = l2.num AND EXISTS (
SELECT
1
FROM
Logs l3
WHERE
l2.id+1 = l3.id AND l2.num = l3.num
)
);