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
| 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:
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.