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