====== Find gaps or duplicates in number sequences in MySql ======
select l.id + 1 as start
from sequence as l
left outer join sequence as r on l.id + 1 = r.id
where r.id is null
select min(l.id + 1) as start
from sequence as l
left outer join sequence as r on l.id + 1 = r.id
where r.id is null
select start, stop from (
select m.id + 1 as start, (select min(id) - 1 from sequence as x where x.id > m.id) as stop
from sequence as m
left outer join sequence as r on m.id = r.id - 1
where r.id is null
) as x
where stop is not null;
select id, count(*) from
sequence
group by id having count(*) > 1;