Skip to content

Commit 2c69730

Browse files
Added Consecutive Numbers
1 parent 1f1a832 commit 2c69730

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Consecutive Numbers
3+
https://leetcode.com/problems/consecutive-numbers/
4+
5+
Write a SQL query to find all numbers that appear at least three times consecutively.
6+
7+
+----+-----+
8+
| Id | Num |
9+
+----+-----+
10+
| 1 | 1 |
11+
| 2 | 1 |
12+
| 3 | 1 |
13+
| 4 | 2 |
14+
| 5 | 1 |
15+
| 6 | 2 |
16+
| 7 | 2 |
17+
+----+-----+
18+
For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.
19+
20+
+-----------------+
21+
| ConsecutiveNums |
22+
+-----------------+
23+
| 1 |
24+
+-----------------+
25+
*/
26+
27+
SELECT DISTINCT(l1.Num) AS ConsecutiveNums
28+
FROM
29+
Logs l1, Logs l2, Logs l3
30+
WHERE (
31+
l1.Num = l2.Num AND l1.Id = (l2.Id + 1) AND
32+
l2.Num = l3.Num AND l2.Id = (l3.Id + 1)
33+
)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ To run a specific problem in your console, go to the file test, add the call to
8181
### Databases
8282
| Problems | Level | Link |
8383
|-|-|-|
84+
| [Consecutive Numbers](/LeetcodeProblems/Databases/Consecutive_Numbers.sql) | Medium | https://leetcode.com/problems/consecutive-numbers |
8485
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary |
8586
| [Exchange Seats](/LeetcodeProblems/Databases/Exchange_Seats.sql) | Medium | https://leetcode.com/problems/exchange-seats |
8687
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |

0 commit comments

Comments
 (0)