Uncompiled / untested / etc... declare type t is table of pls_integer index by pls_integer; d t; begin for i in ( select idx from my_table ) loop d(i.idx) := 1; end loop; for i in d.first .. d.last loop if d.next(i) != i+1 then -- bingo !!!! end if; end loop; end; / So two sweeps...O(2n) depending on the internal implementation of PLSQL tables...