Dev Helpers

SQL / Common table expressions

Find gaps in identifiers

Dev Helpers · March 20, 2026

A gap between identifiers may reflect deletion or allocation history. Locate the gap before assigning it a business meaning.

Worked example

WITH ids(n) AS (VALUES(1),(2),(4)), gap AS (SELECT n,lead(n) OVER(ORDER BY n) AS next FROM ids) SELECT n+1 AS missing FROM gap WHERE next>n+1;

Result

[
  {
    "missing": 3
  }
]

Implementation notes

A missing ID at the start or end is invisible to a comparison between existing neighbours. Use known bounds when the report needs to include edge gaps.

Try it yourself

Test consecutive IDs, a missing middle value and a non-one starting value.

Open the SQL example →

Editorial date March 20, 2026 · Published December 6, 2026

Related articles

Compare two literal lists

Set operations compare rows under SQL's rules for duplicates. Check whether the result represents membership or the number of occurrences.

Count jobs per active day

Grouping timestamps by day produces counts only for days present in the data. A continuous chart may need a calendar series as well.