Problems / Gap Detection in Sequences / Editorial
Use the LEAD window function to pair each ID with the next one in sequence:
LEAD(id) OVER (ORDER BY id)
next_id - id > 1
[id + 1, next_id - 1]
LEAD looks ahead one row in the ordered partition. By comparing each ID to its successor, we can detect discontinuities in a single pass.
a.id + 1 = b.id
Solve Gap Detection in Sequences yourself →