You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Find all numbers that appear at least three times consecutively.
15
+
16
+
Return the result table in any order.
17
+
18
+
The result format is in the following example.
19
+
20
+
21
+
22
+
Example 1:
23
+
24
+
Input:
25
+
Logs table:
26
+
+----+-----+
27
+
| id | num |
28
+
+----+-----+
29
+
| 1 | 1 |
30
+
| 2 | 1 |
31
+
| 3 | 1 |
32
+
| 4 | 2 |
33
+
| 5 | 1 |
34
+
| 6 | 2 |
35
+
| 7 | 2 |
36
+
+----+-----+
37
+
Output:
38
+
+-----------------+
39
+
| ConsecutiveNums |
40
+
+-----------------+
41
+
| 1 |
42
+
+-----------------+
43
+
44
+
"""
45
+
Explanation: 1 is the only number that appears consecutively for at least three times.
46
+
47
+
# Write your MySQL query statement below
48
+
with consecutive_runs as (
49
+
select id, num, lead(num, 1) over (order by id) as lead_num, lag(num, 1) over (order by id) as lag_num
50
+
from Logs
51
+
)
52
+
select distinct num as ConsecutiveNums
53
+
from consecutive_runs
54
+
where num = lead_num and num = lag_num
55
+
56
+
"""
57
+
Explanation
58
+
Common Table Expression (CTE): consecutive_runs
59
+
sql
60
+
Copy
61
+
Edit
62
+
with consecutive_runs as (
63
+
select
64
+
id,
65
+
num,
66
+
lead(num, 1) over (order by id) as lead_num,
67
+
lag(num, 1) over (order by id) as lag_num
68
+
from Logs
69
+
)
70
+
Purpose of the CTE:
71
+
This part creates a temporary result set named consecutive_runs that enriches each row from the Logs table with two extra columns:
72
+
73
+
lead_num: The value of the num column in the next row (based on the id order).
74
+
lag_num: The value of the num column in the previous row (based on the id order).
75
+
Window Functions:
76
+
77
+
lead(num, 1) over (order by id):
78
+
This function returns the value of num from the row immediately following the current one when sorted by id.
79
+
lag(num, 1) over (order by id):
80
+
This function returns the value of num from the row immediately preceding the current one when sorted by id.
81
+
This setup allows us to compare each row with its immediate neighbors.
82
+
83
+
2. Final SELECT Query
84
+
sql
85
+
Copy
86
+
Edit
87
+
select distinct num as ConsecutiveNums
88
+
from consecutive_runs
89
+
where num = lead_num and num = lag_num
90
+
Filtering Condition:
91
+
The WHERE clause checks if the current row's num value is equal to both its next (lead_num) and previous (lag_num) values:
92
+
93
+
num = lead_num
94
+
num = lag_num
95
+
This ensures that the number appears consecutively (at least three times in a row).
96
+
97
+
DISTINCT Keyword:
98
+
The distinct keyword makes sure that each number is listed only once in the final output, even if it occurs in multiple consecutive sequences.
99
+
100
+
Result Column Alias:
101
+
The output column is renamed to ConsecutiveNums for clarity.
102
+
103
+
Summary
104
+
CTE Usage:
105
+
The query first computes additional columns using window functions (lead and lag) to look at neighboring rows.
106
+
107
+
Consecutive Check:
108
+
It then filters out rows where the current value is the same as both the previous and next value, meaning there are at least three consecutive occurrences of that number.
109
+
110
+
Final Output:
111
+
The final result is a list of distinct numbers that appear consecutively in the Logs table.
0 commit comments