Вариант с использованием оконных функции: lag, lead
-- lag
select x.id, x.id_lag
from (
SELECT id,
lag(id, 1) over (order by id) as id_lag
FROM test) x;
select x.id, x.lag_diff
from (
SELECT id,
id - lag(id, 1) over (order by id) as lag_diff
FROM test) x
where x.lag_diff > 1;
-- lead
select x.id, x.id_lead
from (
SELECT id,
lead(id, 1) over (order by id) as id_lead
FROM test) x;
select x.id, x.lead_diff
from (
SELECT id,
lead(id, 1) over (order by id) - id as lead_diff
FROM test) x
where x.lead_diff > 1;
https://www.db-fiddle.com/f/dBpCb4ieea1UQcVPn39QTq/2