Reviewed by Aditya Kumar · Last reviewed 2026-03-24
WITH numbered AS (SELECT num, ROW_NUMBER() OVER (ORDER BY id) rn, num - ROW_NUMBER() OVER (ORDER BY id) AS grp FROM logs) SELECT num FROM numbered GROUP BY num, grp HAVING COUNT(*) >= 3 LIMIT 1. **Why**: Consecutive same numbers share (num - rn); groups with COUNT >= 3 are...
WITH numbered AS (SELECT num, ROW_NUMBER() OVER (ORDER BY id) rn, num - ROW_NUMBER() OVER (ORDER BY id) AS grp FROM logs) SELECT num FROM numbered GROUP BY num, grp HAVING COUNT(*) >= 3 LIMIT 1. Why: Consecutive same numbers share (num - rn); groups with COUNT >= 3 are consecutive. Order by id for sequence. Classic: Gaps-and-islands pattern.
Red Flag: Missing ORDER BY id—assumes order. Pro-Move: 'Gaps-and-islands; we use id for deterministic ordering.'
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.