Knowledge Guide
HomeDatabasesSQL Practice Problems

medium Repeated Values in Sequence

Problem Statement

Table: Sequence

Each row in this table represents a sequence of numbers with a unique ID and a value for each position in the sequence.

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| value       | int     |
+-------------+---------+
id is the primary key for this table.
This table may contain duplicate values in the 'value' column.

Develop a solution to find all values that appear at least three times consecutively in the sequence.

Return the result table in any order.

Example

Input:

Sequence table: +----+-------+ | id | value | +----+-------+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 200 | | 5 | 100 | | 6 | 200 | | 7 | 200 | +----+-------+

Output:

+-----------------+ | RepeatedValues | +-----------------+ | 100 | +-----------------+

In this example, 100 is the only value that appears consecutively for at least three times.

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify all values that appear at least three times consecutively in the Sequence table, we can utilize SQL window functions such as LAG and LEAD. This approach allows us to examine adjacent rows efficiently and determine if a value repeats consecutively across the desired number of positions.

Approach Overview

SQL Query

WITH Consecutive AS ( SELECT value, LAG(value, 1) OVER (ORDER BY id) AS prev_value, LEAD(value, 1) OVER (ORDER BY id) AS next_value FROM Sequence ) SELECT DISTINCT value AS RepeatedValues FROM Consecutive WHERE value = prev_value AND value = next_value;

Step-by-Step Approach

Step 1: Use Window Functions to Access Adjacent Rows

Retrieve each value along with its immediate predecessor and successor to identify potential consecutive repetitions.

SQL Query:

WITH Consecutive AS ( SELECT value, LAG(value, 1) OVER (ORDER BY id) AS prev_value, LEAD(value, 1) OVER (ORDER BY id) AS next_value FROM Sequence )

Explanation:

Output After Step 1:

+-------+------------+------------+ | value | prev_value | next_value | +-------+------------+------------+ | 100 | NULL | 100 | | 100 | 100 | 100 | | 100 | 100 | 200 | | 200 | 100 | 100 | | 100 | 200 | 200 | | 200 | 100 | 200 | | 200 | 200 | NULL | +-------+------------+------------+

Step 2: Identify Values with Three Consecutive Repetitions

Filter the records to find values that are the same as both their preceding and succeeding values, indicating three consecutive occurrences.

SQL Query:

SELECT DISTINCT value AS RepeatedValues FROM Consecutive WHERE value = prev_value AND value = next_value;

Explanation:

Output After Step 2:

+-----------------+ | RepeatedValues | +-----------------+ | 100 | +-----------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Repeated Values in Sequence? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Repeated Values in Sequence** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Repeated Values in Sequence** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Repeated Values in Sequence**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Repeated Values in Sequence**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes