Find Gaps in Sequential IDs

Quick Overview

Write a SQL query that finds every range of missing ids between the smallest and largest id in a table, merging consecutive missing ids into one range. It tests gaps-and-islands reasoning, window functions over ordered rows, and careful handling of range boundaries.

Find Gaps in Sequential IDs

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `records` table stores rows keyed by an integer `id`, and some ids between the smallest and the largest are missing. Find every range of missing `id` values between `MIN(id)` and `MAX(id)`. Return `gap_start` and `gap_end` for each range. A single missing id gives a range whose start and end are equal, and consecutive missing ids are merged into one range. Order the result by `gap_start` ascending. ```hint Where gaps sit Every gap lies between two ids that are present. Look at how far apart neighboring present ids are. ``` ### Input Table(s) `records` | Column | Type | Meaning | |---|---|---| | id | INT | Row identifier | | value | VARCHAR(100) | A label stored with the row; not needed for this task | Primary key: `id` (unique, never NULL). ### Output Contract | Column | Type | Meaning | |---|---|---| | gap_start | INT | First missing id of the range | | gap_end | INT | Last missing id of the range | Return one row per maximal run of consecutive missing ids, ordered by `gap_start` ascending. Ranges never overlap, so `gap_start` is unique and the order is fully determined. No rounding applies. ### Example `records` | id | value | |---|---| | 1 | alpha | | 2 | beta | | 4 | delta | | 7 | eta | | 8 | theta | | 10 | iota | Expected result: | gap_start | gap_end | |---|---| | 3 | 3 | | 5 | 6 | | 9 | 9 | The ids present are 1, 2, 4, 7, 8 and 10. The missing ids are 3, then 5 and 6, then 9, so three ranges come back. The run 5, 6 is one row because the two ids are consecutive. ### Constraints and Clarifications - `records` has at least 1 row. - `id` values are unique and between 1 and 10,000. - Only ids strictly between `MIN(id)` and `MAX(id)` can be missing. Ids below the smallest present id or above the largest one never count, so a table holding ids 5, 6 and 7 returns no rows even though 1 to 4 are absent. - When nothing is missing (for example a one-row table), return an empty result with the two columns. - The `value` column plays no part in the answer.

Overview: Write a SQL query that finds every range of missing ids between the smallest and largest id in a table, merging consecutive missing ids into one range. It tests gaps-and-islands reasoning, window functions over ordered rows, and careful handling of range boundaries.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The records table stores rows keyed by an integer id, and some ids between the smallest and the largest are missing. Find every range of missing id values between MIN(id) and MAX(id). Return gap_start and gap_end for each range. A single missing id gives a range whose start and end are equal, and consecutive missing ids are merged into one range. Order the result by gap_start ascending.

Input Table(s)

records

ColumnTypeMeaning
idINTRow identifier
valueVARCHAR(100)A label stored with the row; not needed for this task

Primary key: id (unique, never NULL).

Output Contract

ColumnTypeMeaning
gap_startINTFirst missing id of the range
gap_endINTLast missing id of the range

Return one row per maximal run of consecutive missing ids, ordered by gap_start ascending. Ranges never overlap, so gap_start is unique and the order is fully determined. No rounding applies.

Example

records

idvalue
1alpha
2beta
4delta
7eta
8theta
10iota

Expected result:

gap_startgap_end
33
56
99

The ids present are 1, 2, 4, 7, 8 and 10. The missing ids are 3, then 5 and 6, then 9, so three ranges come back. The run 5, 6 is one row because the two ids are consecutive.

Constraints and Clarifications

  • records has at least 1 row.
  • id values are unique and between 1 and 10,000.
  • Only ids strictly between MIN(id) and MAX(id) can be missing. Ids below the smallest present id or above the largest one never count, so a table holding ids 5, 6 and 7 returns no rows even though 1 to 4 are absent.
  • When nothing is missing (for example a one-row table), return an empty result with the two columns.
  • The value column plays no part in the answer.