Consecutive Numbers - Problem
Database Query Challenge: Finding Consecutive Patterns

You are given a database table called Logs that contains numerical data with sequential IDs. Your task is to identify all numbers that appear at least three times consecutively in the table.

Table Schema:
Logs table:
โ€ข id (int): Primary key, auto-increment starting from 1
โ€ข num (varchar): The number value to analyze

Goal: Write a SQL query to find all unique numbers that appear in three or more consecutive rows. The order of results doesn't matter.

Example:
If the Logs table contains: [1,1,1,2,1,1,2,2], then number 1 appears three times consecutively (rows 1-3), so it should be included in the result.

Input & Output

example_1.sql โ€” Basic Consecutive Pattern
$ Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+
โ€บ Output: Result table: +-----------------+ | ConsecutiveNum | +-----------------+ | 1 | +-----------------+
๐Ÿ’ก Note: Number '1' appears in rows 1, 2, and 3 consecutively, forming a streak of exactly 3. Number '2' only appears consecutively in rows 6-7 (streak of 2), which doesn't meet our requirement of at least 3.
example_2.sql โ€” Multiple Consecutive Patterns
$ Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | | 6 | 2 | | 7 | 2 | +----+-----+
โ€บ Output: Result table: +-----------------+ | ConsecutiveNum | +-----------------+ | 1 | | 2 | +-----------------+
๐Ÿ’ก Note: Both numbers qualify: '1' appears consecutively in rows 1-3 (streak of 3), and '2' appears consecutively in rows 4-7 (streak of 4). Both meet the minimum requirement of 3 consecutive appearances.
example_3.sql โ€” No Consecutive Pattern
$ Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 2 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | +----+-----+
โ€บ Output: Result table: +-----------------+ | ConsecutiveNum | +-----------------+
๐Ÿ’ก Note: No number appears 3 times consecutively. The pattern alternates between 1 and 2, so the maximum consecutive count for any number is 1. The result table is empty.

Constraints

  • 1 โ‰ค Logs table size โ‰ค 104
  • id is a unique auto-increment integer starting from 1
  • num is stored as varchar but contains numeric values
  • Consecutive means the IDs must be sequential (no gaps)
  • Result should contain unique numbers only (use DISTINCT)

Visualization

Tap to expand
Logs Table Sequenceid: 1num: 1id: 2num: 1id: 3num: 1id: 4num: 2id: 5num: 1id: 6num: 2Window 1: Match Found!Values: 1, 1, 1โœ“ All identical - Record '1'Window 2: No MatchValues: 1, 1, 2โœ— Not identical - SkipFinal ResultConsecutiveNum1
Understanding the Visualization
1
Position Window
Place a 3-element window starting at the first row
2
Compare Values
Check if all three values in the window are identical
3
Slide Window
Move window one position forward and repeat
4
Collect Results
Record any number that creates a 3+ consecutive match
Key Takeaway
๐ŸŽฏ Key Insight: Window functions eliminate the need for expensive self-joins by allowing us to access neighboring rows directly, making consecutive pattern detection both elegant and efficient.
Asked in
Meta 45 Amazon 35 Microsoft 28 Google 22
28.4K Views
High Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen