SQL Fiddle Setup and Solution
1. Step1
From the list of IDs get the current id and all the next ids available
select l1.id curr_id,l2.id next_id from
id_list l1,id_List l2
where l1.id < l2.id;
2. Step 2
From the above list we will see all the combinations but filter only the one combination per current ID with immediate smallest next ID and for that, get min current ID and min next ID for each current ID. Use group by per current ID
with id_combinations as
(
select l1.id curr_id,l2.id next_id from
id_list l1,id_List l2
where l1.id < l2.id
)
select min(curr_id)+1 missing_id_start -- Need to add 1 from current available id
,min(next_id)-1 missing_id_end -- Need to subtract 1 from next available id
from id_combinations
group by curr_id
having min(curr_id)+1 < min(next_id) -- Filter to get only the missing ranges
lag
andlead
(maybe when I have time at lunch)... but that would be specific to Oracle (or others that support those functions). Was this to be a generic solution that can run on any RDBMS, or are you allowed to assume a specific implementation? – Pulpwood